Ann: pipes-break

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Ann: pipes-break

David McBride
About five months ago I had the sudden need to be able to split bytestrings and or text by delimiters that were wider than one character, but as far as I know nothing like that existed on hackage.  I went back and forth with Michael Thompson about it, but I was not able to make anything useful at the time.  But recently I was able to create a library that does exactly that, right here:

Much thanks goes to Gabriel for his clutch inclusion at the bottom of the pipes-parse tutorial about how to parse a producer while having a producer as its base monad.  That blurb made it all come together for me.

The library has functions for both text and bytestring like

breaksBy :: Monad m => ByteString -> Producer ByteString m r -> FreeT (Producer ByteString m) m r
replace :: Monad m => ByteString -> ByteString -> Producer ByteString m r -> Producer ByteString m r
breaksBy :: Monad m => ByteString -> Lens' (Producer ByteString m r) (FreeT (Producer ByteString m) m r)

which allow you to do things like the following with any delimiters of your choice

mconcat $ toList $ replace "\r\n" "\n" (P.each ["ab","c\nd","","ef","\r\nfo","o\n\r","\nbar"])

I've gone to great pains to make it as efficient as I know how to and it has some tests to verify that it is correct.  Nonetheless if anyone finds any bugs or documentation problems, I'll be happy to fix them.

I'm also open to any omissions that make sense, so long as they make sense in the context of this package.  I have half formed visions for a few already like find, elem, and count, groupBy, as well as various parsing functions that should now be doable, but I haven't though very hard about what types they would have.