[erlang-questions] generators/iterators
Damien Morton
dmorton@REDACTED
Mon Jun 18 08:10:38 CEST 2007
I forgot - functional programming - a function cant carry internal and
mutable state. Each call to a function _should_ return the same value.
Your scheme will work where mine will not.
> You can easily make a lazy stream by creating a function
> which returns the next value and a fun.
>
> For example a generator for an infinite stream of integers might look
> like this
>
> intsFrom(I) ->
> {I, fun() -> intsFrom(I+1) end}.
>
> If you call intFrom(1) it will return {1, F1}
> if you evaluate F1() it will return {2, F2}
> if you evaluate F2() is will return {3, F3} ... and so on
>
> Or use a process
>
> intsFrom(I) ->
> receive
> {Pid, next} ->
> Pid ! I,
> intsFrom(I+1)
> end.
>
> Pid request the next integer by sending a {self(), next} message to
> the generator
>
>
>
> /Joe
>
>
>
> On 6/18/07, Damien Morton <dmorton@REDACTED> wrote:
>> Umm, I just realised that a stream/generator would need to be a function
>> that returns a function that can be called repeatedly to generate the
>> sequence, probably terminating with a well-known sequence terminating
>> exception.
>>
>> This would solve the problem of the generators needing to be reset
>> during nested loops in a comprehension.
>>
>> > Hmm,
>> >
>> > Coming from a strong Python and C# background, I find myself missing
>> > generators/iterators.
>> >
>> > For example, when using list comprehension syntax, I find myself
>> wanting
>> > to use a function as a generator
>> >
>> > items(X) ->
>> > fun() -> next_item(X) end.
>> >
>> > [Y*Y || Y <- items(X)]
>> >
>> > Would it be simple to have erlang recognise a function as a generator?
>> >
>> > Sorry if the terminology is getting mixed up here.
>> > _______________________________________________
>> > erlang-questions mailing list
>> > erlang-questions@REDACTED
>> > http://www.erlang.org/mailman/listinfo/erlang-questions
>> >
>> >
>> >
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>
>
More information about the erlang-questions
mailing list