[Haskell-cafe] Iteratee question
Valery V. Vorotyntsev
valery.vv at gmail.com
Fri Nov 20 10:12:59 EST 2009
I am writing a binary data parser and use `iteratee' package.
The following pattern appears quite often in my code:
> results <- map someConversion `liftM` replicateM nbytes Iter.head
The meaning is: take `nbytes' from stream, apply `someConversion' to
every byte and return the list of `results'.
But there's more than one way to do it:
> i1, i2, i3 :: Monad m => Int -> IterateeG  Word8 m [String]
> i1 n = map conv `liftM` replicateM n Iter.head
> i2 n = map conv `liftM` joinI (Iter.take n stream2list)
> i3 n = joinI $ Iter.take n $ joinI $ mapStream conv stream2list
If you try the program I've hpasted, you will see that these three
iteratees have equal results.
Of those i1, i2, i3 which one is "better" and why? Or is there another -
preferable - way of applying iteratees to this task?
My naïve guess is that i1 will have worse performance with big n's. It
looks like `i1' is reading bytes one by one, while `i2' takes whole
chunks of data... I'm not sure though.
I would appreciate it a lot if you improve my understanding.
More information about the Haskell-Cafe