diff options
Diffstat (limited to 'examples/decode.hs')
-rw-r--r-- | examples/decode.hs | 30 |
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 | |||
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 | -} \ No newline at end of file | ||