Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Foad Ardalan
    @techla
    why should we map twice ?
    the a is on a positive position If I understood it correctly
    Gavin Bisesi
    @Daenyth
    If you want to get from String -> Parser String to Char -> Parser Char then you need imap because you need to convert Char input to String and then String output to Char
    *In the general case
    if it's literally just String and Char and not arbitrary type arguments then hardcoding some conversion would work
    Foad Ardalan
    @techla
    the goal is to go from String -> Parser (Array Char) to String -> Parser String using fromCharArray :: Array Char -> String
    I think I understood the prroblem
    If I had a type defined as newtype ReaderParser a = ReaderParser (String -> Parser a)
    I could make the map in one go
    only one map
    Gavin Bisesi
    @Daenyth
    yes if String is fixed then it's simpler
    yeah that's just map using whatever Kleisli looks like in ps.
    it looks like >=> I believe
    Foad Ardalan
    @techla
    this since there’s no indication on witch type it is polymorphic, we have too go twice
    yes
    Klisli arrows are just like haskell ones
    can It help here ?
    Gavin Bisesi
    @Daenyth
    String -> Parser (Array Char) is ~= Kleisli Parser String (Array Char), so map on that gets you to Kleisli Parser String String
    I don't know enough of ps/haskell to give the most succinct way to do that
    Foad Ardalan
    @techla
    ok I will think about it.
    i need some minutes to wrap my mind around what you've just said
    Gavin Bisesi
    @Daenyth
    I'll try a tldr

    FP is all about composition - how to combine expressions to larger ones that mean what you want.

    Mathematical functions are like A => B and then compose really nicely, A=>B and B=>C compose to give you A => C

    Category theory is the study of composition, generalizes that notion of composition. It notices that a -> f b shaped functions can still compose for some f - Monads. a -> f b and b -> f c give you a -> f c - that's flatMap/bind/>>=/whatever you call it

    The "Kleisli" category is the name for the category of composition on that shape.

    Kleisli types, aka "reader monad" or ReaderT more generally, are how we encode that in FP languages.

    Where a -> b is a monad mapping on b, if you tried using that -> monad for a -> f b you'd be mapping on f b, not b. Kleisli/ReaderT shapes let you change the monad being used so that given shapes like a -> f b, you map on the b

    So since String -> Parser (Array Char) is such a shape / a=String f=Parser b=Array Char, then you can use Kleisli/ReaderT to recover the ability to map on the Array Char part, which is exactly the operation you want
    Foad Ardalan
    @techla
    @Daenyth here is the full implementation I work on
    Gavin Bisesi
    @Daenyth
    anyway gtg shortly but I hope that helps
    it compiles
    and it works modulo the jsonArray and the jsonObject
    Gavin Bisesi
    @Daenyth
    there we go, Control.Monad.Reader.Trans is the place to look for that shape
    It's called ReaderT in ps. I'm used to scala where the newtype is called Kleisli and ReaderT is just an alias for it
    Foad Ardalan
    @techla
    I’m trying to clean, optimize it and make it works with those two last functions
    ReaderT is the Reader Transformer if I don’t get it wrong
    it’s for monad composition
    Foad Ardalan
    @techla
    @Daenyth thank you for your help
    I have a little category theory background and I really want to make it work with it
    I will explore the solution you presented to me
    thank you again
    Gavin Bisesi
    @Daenyth
    on the other hand though it's just one line of code, might not be worth it unless this is a widespread thing
    Foad Ardalan
    @techla
    it’s a good exercise anyway
    Gavin Bisesi
    @Daenyth
    Writing naive and straightforward code is often best unless you're sure the fancy stuff pays off enough for how much it costs other developers to read
    Foad Ardalan
    @techla
    @Daenyth it’s actulally as simple as stringP :: String -> Parser String stringP = (map (fromCharArray)) <<< sequence <<< (map charP) <<< toCharArray :D
    I get it wrong since the very begining
    Sean Yu
    @seanyu4296
    @techla afaik, Show instances are not derivable. What you can do is derive instance generic then use genericShow
    import Data.Generic.Rep as GR
    import Data.Generic.Rep.Show (genericShow)
    
    data IntOrBoolean2
      = Int2 Int
      | Boolean2 Boolean
    
    -- note the underscore at the end for the `rep` parameter of class Generic
    derive instance genericIntOrBoolean2 :: GR.Generic IntOrBoolean2 _
    
    instance showIntOrBoolean2 :: Show IntOrBoolean2 where
      show = genericShow
      -- now we get a Show
    Foad Ardalan
    @techla
    @seanyu4296 thank you
    here the Rep stands for Representable ?
    ie Functors that are isomorphique to the Home Functor ?
    Sean Yu
    @seanyu4296
    I think so Rep is representable