]> git.immae.eu Git - github/fretlink/text-pipes.git/blob - examples/decode.hs
8cb44f8443ebb4459ed3f617f5df4f700c8ac022
[github/fretlink/text-pipes.git] / examples / decode.hs
1 -- http://www.haskellforall.com/2014/02/pipes-parse-30-lens-based-parsing.html
2
3 import Data.ByteString (ByteString)
4 import Data.Text (Text)
5 import Lens.Family.State.Strict (zoom)
6 import Pipes
7 import Pipes.Parse
8 import qualified Pipes.ByteString as ByteString
9 import qualified Pipes.Text as Text
10
11 -- Retrieve all `Text` chunks up to 10 characters
12 parser :: Monad m => Parser ByteString m [Text]
13 parser = zoom (Text.decodeUtf8 . Text.splitAt 10) drawAll
14
15 main = do
16 (textChunks, leftovers) <- runStateT parser ByteString.stdin
17 print textChunks
18
19 -- Now print the remaining `ByteString` chunks
20 byteChunks <- evalStateT drawAll leftovers
21 print byteChunks
22 {-
23 $ ./decode
24 Hello, 世界!!!<Enter>
25 ["Hello, \19990\30028!"]
26 abcdefg<Enter>
27 <Ctrl-D>
28 ["!!\n","abcdefg\n"]
29
30 -}