aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/decode.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/decode.hs')
-rw-r--r--examples/decode.hs30
1 files changed, 30 insertions, 0 deletions
diff --git a/examples/decode.hs b/examples/decode.hs
new file mode 100644
index 0000000..8cb44f8
--- /dev/null
+++ b/examples/decode.hs
@@ -0,0 +1,30 @@
1-- http://www.haskellforall.com/2014/02/pipes-parse-30-lens-based-parsing.html
2
3import Data.ByteString (ByteString)
4import Data.Text (Text)
5import Lens.Family.State.Strict (zoom)
6import Pipes
7import Pipes.Parse
8import qualified Pipes.ByteString as ByteString
9import qualified Pipes.Text as Text
10
11-- Retrieve all `Text` chunks up to 10 characters
12parser :: Monad m => Parser ByteString m [Text]
13parser = zoom (Text.decodeUtf8 . Text.splitAt 10) drawAll
14
15main = 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
24Hello, 世界!!!<Enter>
25["Hello, \19990\30028!"]
26abcdefg<Enter>
27<Ctrl-D>
28["!!\n","abcdefg\n"]
29
30-} \ No newline at end of file