[Haskell-cafe] compare iteratee with python's yield

yi huang yi.codeplayer at gmail.com
Fri Jul 1 09:12:37 CEST 2011


I just read several tutorials on iteratee, i find that iteratee is similar
to python's generator, both allow streamlined data processing. For example,
i can implement enumFile and printChunks in python like this:

    EOF = None
    def enum_file(bufsize, filename):
        with open(filename) as input:
            while True:
                data = input.read(bufsize)
                if not data:
                    break
                yield data
        yield EOF

    def print_chunks(print_empty, generator):
        for chunk in generator:
            if chunk==EOF:
                print 'EOF'
                return
            if len(chunk)==0 and not print_empty:
                continue
            print chunk

    print_chunks(True, enum_file(2, "data"))

But i find iteratee far more complicated than python's generator, is that
because iteratee can do something python's generator can't, or i simply need
to be more familar with functional programming style.



-- 
http://www.yi-programmer.com/blog/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110701/fd531acc/attachment.htm>


More information about the Haskell-Cafe mailing list