[erlang-questions] iterators in Erlang

Robert Raschke rrerlang@REDACTED
Wed Oct 25 16:51:40 CEST 2006


James wrote:
> I've recently been brushing up on my Python, because I'm teaching a
> class using it.  What's surprising to me is how much modern Python
> relies on iterators (an "iterator" being an object that returns one
> value at a time in a sequence).  Where lists:seq(1,100000) returns a
> large list in erlang, xrange(1,100000+1) in Python generates one value
> at a time.  Then you can sum these values with sum(xrange(1,100000+1))
> and there's still no intermediate list.
> 
> Even more interesting is that iterators have evolved into generators,
> which are a poor man's processes).  People are twisting generators to
> get cooperative multitasking.

I wonder if someone from the Icon (http://www.cs.arizona.edu/icon/intro.htm)
team has migrated to Python.  Icon seems to have gone down the most
rigorous route in using generators, co-expressions, and goal-oriented
evaluation.  Fabulous language that in my ideal world would have
pre-empted all the perl/php/*script/python cruft that came afterwards.

> What jumps out at me is how natural this stuff is to do in Erlang, and
> yet the iterator/generator style isn't an inherent part of the Erlang
> libraries.  I'm not sure if it really *should* be, of course, but I
> was curious why Erlang never went down this path.

I'm not up to date with Python, but what kinds of control structures
does it provide enumerate values from a generator?  Is it the OO
iterator concept of having to explicitly call some kind of next()
method?  In that case there appears not to be any obvious advantage
over "just" writing processes in Erlang to generate values and
communicating with messages.

Robby





More information about the erlang-questions mailing list