chpatrick / codec

Easy bidirectional serialization in Haskell
BSD 3-Clause "New" or "Revised" License
50 stars 6 forks source link

Add applicative operator #4

Closed Lysxia closed 7 years ago

Lysxia commented 7 years ago

I think this new (=.) allows to create Codecs for records neatly without Template Haskell.

Example:

-- As if    data (,) a b = (,) { fst :: a, snd :: b }

parsePair :: Codec fr fw a -> Codec fr fw b -> Codec fr fw (a, b)
parsePair a b
  = (,)
  <$> fst =. a
  <*> snd =. b

parseFlipped :: Codec fr fw b -> Codec fr fw a -> Codec fr fw (a, b)
parseFlipped b a
  = flip (,)
  <$> snd =. b
  <*> fst =. a

If you agree with this I can try to document it in Data.Codec.

(.=) seems to be the most common choice to map fields in other serialization libraries (JSON, YAML), and I picked the hardly used (=.) to avoid conflicts (assuming that users of Codec might need to use one of these other libraries at the same time).

Lysxia commented 7 years ago

What do you think of adding a Profunctor instance as well?