[squeak-dev] Generators

Frank Shearar frank.shearar at gmail.com
Wed Apr 12 15:40:02 UTC 2017


FWIW I think this is a good change to make.

frank

On Apr 12, 2017 06:43, "Bert Freudenberg" <bert at freudenbergs.de> wrote:

> Hi all,
>
> there was an interesting question about Generators on StackOverflow:
>
> http://stackoverflow.com/questions/43340007/yield-prolog-adaptation-for-
> smalltalk
>
> It's about a pseudo-Prolog implementation using Generators and yield.
>
> Our Generator *almost* works for this, except there is a little problem
> with the timing of side-effects:
>
> | x gen |
> x := 0.
>
> gen := Generator on: [:g |
> x := 1.
> g yield: nil.
>
> x := 2.
> g yield: nil.
>
> x := 3.
> g yield: nil].
>
> gen do: [:i | Transcript cr; show: x].
>
> This *should* print '1 2 3' but actually prints '2 3 3'. This is because
> our Generator is written to support a streaming interface (using atEnd and
> peek) so it always computes one yield ahead.
>
> In other languages (e.g. JavaScript, Python, C#) this is different, the
> execution is paused at the current yield statement, not 'before' the next
> one. In general, a generator cannot know if there will be more "yields"
> without executing more code. In Javascript, 'next' returns both a value and
> a 'done' flag, and if the flag is true, the returned value must not be used.
>
> IMHO we should fix this - see attached changeset Marcel and I came up
> with. We did not fix the tests yet, but the behavior seems right.
>
> Even though generators are in the image for a long time they are not used
> widely yet (AFAIK) so maybe we can still change the behavior? What do
> people think?
>
> - Bert & Marcel -
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20170412/bc2f1624/attachment.html>


More information about the Squeak-dev mailing list