[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[1]:

> 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

  [1] http://en.wikipedia.org/wiki/There's_more_than_one_way_to_do_it

If you try the program[2] I've hpasted, you will see that these three
iteratees have equal results.

  [2] http://hpaste.org/fastcgi/hpaste.fcgi/view?id=12464#a12464

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 mailing list