[erlang-questions] Python Generators In Erlang and constant memory usage

Ulf Wiger ulf.wiger@REDACTED
Fri Mar 20 08:56:53 CET 2009


Frederic T-H wrote:
> First time posting here, I've been reading this mailing lists for a few 
> months now, programming Erlang for a few months too.
> 
> Python and its most functionnal parts is what eventually brought me to 
> Erlang. One feature that I loved in Python and instantly noticed was not 
> available in Erlang was generators. Generators are basically normal 
> functions, except they return an iterator, letting you fetch each result 
> one by one, effectively letting you work with constant memory. An 
> example of the way Python does it:
> 
>    def gen(n):
>        i = 0
>        while i<n:
>            yield i
>            i+=1
>        return
> 
>    x = gen(2)
>    x.next() # returns 0
>    x.next() # returns 1
>    x.next()  # throws an exception

This feels a bit reminiscent of lazy lists, which were
discussed at some length on the erlang list quite
recently (the year 2000, in fact):

http://erlang.org/pipermail/erlang-questions/2000-October/001875.html

BR,
Ulf W
-- 
Ulf Wiger
CTO, Erlang Training & Consulting Ltd
http://www.erlang-consulting.com



More information about the erlang-questions mailing list