help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] Continuations and generators


From: Topher Cyll
Subject: Re: [Help-smalltalk] Continuations and generators
Date: Tue, 5 Jun 2007 13:26:56 -0400

I was holding back, since I'm not a core user of gst or anything, just
a fan.  But the list seems quiet, so...

Including them sounds smart to me. Generators are nice to have around,
and having the full power of continuations available in the main image
seems like a good foundation for the future.

Toph




On 6/4/07, Paolo Bonzini <address@hidden> wrote:
In the last few days I've been playing with continuations and
generators.  I think I now understand how they work pretty well, and I
could make the generator example pretty fast using continuations (much
to my surprise, 5 times faster than with processes).

Like in Python, generators are a quick way to make a Stream object out
of a block, like this:

     ^Generator on: [ :gen |
         | i |
         i := 1.
         [ gen yield: i. i := i + 1 ] repeat ]

This would return an infinite stream giving the natural numbers.  You
can use any finite prefix of it with something like "gen next: 10"
(returning the first 10 numbers).  More interesting example:

     Generator on: [ :gen |
         | a b c |
         a := b := 1.
         [ gen yield: a. c := a + b. a := b. b := c ] repeat ].

You can also have finite generators, as simple as

     Generator on: [ :gen | gen yield: 1 ].

or even

     Generator on: [ :gen | ].


Other example include creating stream decorators, like the ones that
2.3.5 moved into the main image.

     Stream >> lines [
         ^Generator on: [ :gen |
             [ self atEnd ] whileFalse: [ gen yield: nextLine ] ] ]

     Stream >> select: aBlock [
         ^Generator on: [ :gen || obj |
             [ self atEnd ] whileFalse: [
                 obj := self next.
                 (aBlock value: obj) ifTrue: [ gen yield: obj ] ] ] ]

(Of course the performance would be worse than with custom streams, but
for scripting usage there could be an advantage).

I would like to know if there is interest in moving generators (and, as
a prerequisite, continuations) to the main image.

Paolo


_______________________________________________
help-smalltalk mailing list
address@hidden
http://lists.gnu.org/mailman/listinfo/help-smalltalk





reply via email to

[Prev in Thread] Current Thread [Next in Thread]