So I also just updated `pipes-binary` with a `lenses` branch, too.  You can 
find the code for it here:

https://github.com/Gabriel439/pipes-binary/tree/lenses

It has a tutorial at the bottom of the main module, and I think the code 
example it contains helps give an idea of typical usage patterns:

    import Data.ByteString (ByteString)
    import Lens.Family.State.Strict (zoom)
    import Pipes
    import Pipes.Binary
    import Pipes.Parse
    import qualified Pipes.Prelude as P
    import Prelude hiding (splitAt)
   
    readInts :: Int -> Producer Int IO ()
    readInts n = P.readLn >-> P.take n
   
    encodedValues :: Producer ByteString IO ()
    encodedValues = do
        for (readInts 3) encode  -- Encode 3 Ints read from user input
        encode 'C'               -- Encode a 'Char'
        encode True              -- Encode a 'Bool'
    -- example.hs
   
    decoder :: Parser ByteString IO ()
    decoder = do
        xs <- zoom (decoded . splitAt 3) drawAll      -- Decode up to three 
'Int's
        lift $ print (xs :: [Int])
        y  <- decode                                  -- Decode a single 
'Char'
        lift $ print (y :: Either DecodingError Char)
        z  <- zoom decoded draw                       -- Same as 'decode', 
but
        lift $ print (z :: Maybe Bool)                -- with a 'Maybe'
   
    main = evalStateT decoder encodedValues

If you provide the full input it functions properly:

    $ ./example
    1<Enter>
    2<Enter>
    3<Enter>
    [1,2,3]
    Right 'C'
    Just True

... and it even does the right thing if you terminate the input early.  Any 
bytes that do not parse successfully are correctly preserved for subsequent 
parsers.

    $ ./example
    <Ctrl-D>
    []
    Right 'C'
    Just True

-- 
You received this message because you are subscribed to the Google Groups 
"Haskell Pipes" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].

Reply via email to