[erlang-questions] iterators in Erlang

Torbjorn Tornkvist <>
Wed Oct 25 23:32:52 CEST 2006


Just for the fun of it.
Here is some old code playing with the 'stream' concept:

  http://forum.trapexit.org/viewtopic.php?p=22424#22424

Makes it possible to write expressions such as:


1> streams:first(10, streams:primes()).
[2,3,5,7,11,13,17,19,23,29]

2> hd(lists:reverse(streams:first(2000, streams:pi()))).
3.14399


Cheers, Tobbe


James Hague skrev:
> 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.
> 
> 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.
> 
> James
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 




More information about the erlang-questions mailing list