Walter: any plan to add support for yield/return Iterators/generators?

Steve Horne stephenwantshornenospam100 at aol.com
Wed Sep 6 07:00:44 PDT 2006


On Tue, 05 Sep 2006 21:24:39 -0400, Marcio <mqmnews123 at sglebs.com>
wrote:

>Walter Bright wrote:
>>> One of the things needed for sure is heap frames.
>> 
>> That's right, instead of using the stack for the locals, the heap is 
>> used. It isn't that much different from using a functor. In fact, now 
>> that I think about it, a functor might be a better way to do it than yield.
>
>
>	Smalltalk blocks, basically.

I don't think so.

For each instance of a generator, you need a complete separate stack.
That stack is kept on the heap, separate from the normal processor
stack. Both have to be full stacks, since both contexts need to
support function calls - even recursion - independently. Even though
the generator itself is likely to be a single function with yields
done directly from itself, it still needs to call normal functions to
do its stuff.

It's a long time since I used smalltalk, and I never used it much
anyway, but aren't blocks basically a kind of closure thing? That is,
they hold parameters and locals in heap-allocated memory, but without
stacking support - just single fixed-size frame?

I seem to remember it as some kind of
anonymous-functions-with-closures hack.


By the way...


In Python, you can write something like this...

  x = First (i for i in xrange(100) if Acceptable (i))

This bit...

  i for i in xrange(100) if Acceptable (i)

...is a generator expression, and closely related to the following
list comprehension...

  [i for i in range(100) if Acceptable (i)]

Which builds a list of all 'acceptable' values of i in the range 0 to
99 inclusive. The generator expression differs in that it only
generates values as they are needed. It doesn't build the whole list.
It is 'lazy'.

The 'First' function would be written as...

  def First (p) :
    return p.next ()

That only generates enough results to find the first acceptable value.

Personally, I think Pythons generator expressions are an extremely
important language feature. They remind me of the language that, to
me, defines generator-obsessive programming - Icon. But Pythons
generator expressions have a much more intuitive syntax, since Python
doesn't obsess - it simply provides.

My vote is that, if D gets generators, it should also get some kind of
generator expression as a way of creating anonymous generators.

But first, Walter needs to concentrate on achieving immortality, so he
has time to do all this ;-)

-- 
Remove 'wants' and 'nospam' from e-mail.



More information about the Digitalmars-d mailing list