io - Using Pipes to read and write binary data in Haskell -


i trying read , write many ints in constant memory. have figured out how write ints memory have not figured out how read them back.

import control.lens (zoom) import system.io (iomode(..), withfile) import pipes import qualified pipes.prelude p import qualified pipes.bytestring pb     import qualified pipes.parse p import qualified pipes.binary p  intstream :: monad m => proxy x' x () int m b intstream = go (0 :: int)    go = yield >> go (i + 1)  decoder :: monad m => int ->  p.parser p.bytestring m [int] decoder n = zoom (p.decoded . p.splitat n) p.drawall  main :: io () main =     withfile "ints" writemode $ \h ->          runeffect $ intstream p.encode >-> p.take 10000 >-> pb.tohandle h     withfile "ints" readmode $ \h ->          xs <- p.evalstatet (decoder 10000000) (pb.fromhandle h)          print xs 

i got decoder function documentation pipes.binary. uses drawall according documentation drawall not idiomatic use of pipes , provided testing purposes.

my question how modify decoder doesn't use drawall , not load values of xs memory. instead of printing list of xs p.map print on stream of decoded ints being read file.

the docs decoded lens stream of bytes stream of decoded values. can latter out of former using view lens:

decoder :: monad m => int -> producer p.bytestring m -> producer int m () decoder n p = void (view p.decoded p) >-> p.take n  main :: io () main =     withfile "ints" writemode $ \h ->          runeffect $ intstream p.encode >-> p.take 10000 >-> pb.tohandle h     withfile "ints" readmode $ \h ->          runeffect $ decoder 10000 (pb.fromhandle h) >-> p.print 

i don't have experience pipes, followed types here. program seems function intended though.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -