[Haskell-cafe] Iteratee: manyToOne

Dmitry Olshansky olshanskydr at gmail.com
Fri Apr 29 18:31:33 CEST 2011


In my case leftover is not important.

But in common case... Just an idea...

What if we provide
iterWhile :: Iteratee a m () -> Iteratee a m b -> Iteratee a m b

The first Iteratee only control when the result should be yeilded and feed
an input to second Iteratee.

Then we can change manyToOne to
manyToOne' :: Iteratee a m () -> [Iteratee a m b] -> Iteratee a m [b]
manyToOne' iw = iterWhile iw . manyToOne







2011/4/29 Felipe Almeida Lessa <felipe.lessa at gmail.com>

> On Fri, Apr 29, 2011 at 6:32 AM, John Lato <jwlato at gmail.com> wrote:
> > If you do this, the user needs to take care to order the iteratees so
> that
> > the last iteratee has small leftovers.  Consider:
> >
> > manyToOne [consumeALot, return ()]
> >
> > In this case, the entire stream consumed by the first iteratee will need
> to
> > be retained and passed on by manyToOne.  In many cases, the user may not
> > know how much each iteratee will consume, which can make these semantics
> > problematic.
> >
> > Iteratee has 'enumPair', (renamed 'zip' in HEAD) which returns the
> leftovers
> > from whichever iteratee consumes more.  This avoids the problem of
> retaining
> > extra data, and seems simpler to reason about.  Although if you really
> need
> > to consume a predictable amount of data, the safest is probably to run
> the
> > whole thing in a 'take'.
>
> My motivation is: in general it is difficult (impossible?) to choose
> the iteratee that consumed more data because you don't know what the
> data is.  For example, if you give 'Chunks [a,b]' to two iteratees and
> one of them returns 'Chunks [c]' and the other one returns 'Chunks
> [d]', which one consumed more data?  The answer is that it depends on
> the types.  If they are Ints, both consumed the same, if they are
> ByteStrings, you would need to check if one is prefix of the other.
> What if one returns 'Chunks [c]' and the other one returns 'Chunks
> [d,e]'?  If they are ByteStrings, should we compare 'c' against 'd ++
> e'?
>
> So I thought it would be easier to program with an API that is
> predictable and immune to changes in block sizes.  If you don't want
> leftovers, just use 'manyToOne [..., dropWhile (const True)]', which
> guarantees that you won't leak.
>
> Cheers,
>
> --
> Felipe.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110429/9ea38b5e/attachment.htm>


More information about the Haskell-Cafe mailing list