]> git.immae.eu Git - github/fretlink/text-pipes.git/blob - examples/decode.hs
updated examples
[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
7 import Pipes
8 import Pipes.Parse
9 import qualified Pipes.ByteString as ByteString
10 import qualified Pipes.Text as Text
11 import qualified Pipes.Text.Encoding as Text
12
13 -- Retrieve all `Text` chunks up to 10 characters
14 parser :: Monad m => Parser ByteString m [Text]
15 parser = zoom (Text.utf8 . Text.splitAt 10) drawAll
16
17 main = do
18 (textChunks, leftovers) <- runStateT parser ByteString.stdin
19 print textChunks
20
21 -- Now print the remaining `ByteString` chunks
22 byteChunks <- evalStateT drawAll leftovers
23 print byteChunks
24 {-
25 $ ./decode
26 Hello, 世界!!!<Enter>
27 ["Hello, \19990\30028!"]
28 abcdefg<Enter>
29 <Ctrl-D>
30 ["!!\n","abcdefg\n"]
31
32 -}