[squeak-dev] Generators
Bert Freudenberg
bert at freudenbergs.de
Thu Apr 13 11:11:38 UTC 2017
On Thu, Apr 13, 2017 at 10:42 AM, Levente Uzonyi <leves at caesar.elte.hu>
wrote:
> It has nothing to do with the halting problem, because #atEnd don't have
> to tell if your generator will ever stop yielding or not. It should just
> tell if the upcoming #next send will return something meaningful or not.
> The current implementation does exactly that.
>
Yes, but at the price of executing up to the next yield in advance. That's
the only way it can know if the "upcoming #next send will return something
meaningful or not".
Here's another example:
>
> | generator |
> generator := Generator on: [ :g | g yield: 1 ].
> generator next; atEnd
> "==> false"
>
In the current implementation it returns true.
In the new one, it returns false, you will have to send #next another time
and check atEnd to see if the value returned from #next is an actual value,
or not. This is the reason why I posted the JavaScript example - it returns
the value plus the flag if the value is valid.
My point is that the stream-like API forces you to read one object ahead.
> Which is why I suggested rewriting #do: to not use it.
We did rewrite #do: so it does the right thing. Are you suggesting we could
rewrite #do: in the old implementation to do the right thing? That would be
pretty awesome.
In the end, what I want is this:
| gen out x |
x *:=* 0.
gen *:=* Generator on: [ :g |
x *:=* 1.
g yield: 'A'.
x *:=* 2.
g yield: 'B'.
x *:=* 3.
g yield: 'C'].
out *:=* ''.
gen do: [ :s | out *:=* out, x, s].
out = '1A2B3C'.
"==> true"
If we can make that happen by slightly modifying the current
implementation, great. I just don't know how to do it (other than the
changeset we posted).
- Bert -
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20170413/7090dd9d/attachment.html>
More information about the Squeak-dev
mailing list
|