Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Pipes.Break.Text.Lens
Description
For those who like lenses, here are Lens library versions of the functions from Pipes.Break.Text
To learn more about using lenses to manipulate pipes, check out the in depth tutorial at http://hackage.haskell.org/package/pipes-group/docs/Pipes-Group-Tutorial.html
Due to the fact that the endsBy family of functions are non invertible, it doesn't make much sense to encourage their use as lenses. For example, if your protocol were blocks of lines delimited by a double newline at the end (such as in email or http).
>>>
fmap mconcat <$> P.toListM $
>>>
over (endsBy "\n\n" . individually) (over (endsBy "\n" . individually) id)
>>>
(yield "A\nB\n\nA\nB\nC\n\n")
"A\nB\n\n\nA\nB\nC\n\n\n\n\n"
As you can see, this would result in the wrong number of newlines being appended when reforming.
Grouping producers by a delimiter
>>>
fmap mconcat <$> P.toListM $
>>>
over (breaksBy "\n\n" . individually) (over (breaksBy "\n" . individually) (<* yield "!"))
>>>
(P.each ["A\nB","\n\n","A","","\nB\nC","\n\n"])
"A!\nB!\n\nA!\nB!\nC!\n\n"