generators

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

generators

Eliot Miranda-2
Hi All,

    I juts had occasion to use generators in anger for the first time.  I was trying to fnd out why a clone of a prse tree was reporting that it was not the same tree.  Generators made enumerating all nodes in each tree together trivial:

| them |
them := Generator on: [:g| newTree nodesDo: [:n| g yield: n]].
parseTree nodesDo:
[:n| | twin |
twin := them next.
(n isSameAs: twin) ifFalse:
[self halt]]

My message though comes from noticing that generators yield their values via next, so why not use nextPut: instead of yield: ?

e.g. I think this is better:

| them |
them := Generator on: [:g| newTree nodesDo: [:n| g nextPut: n]].
parseTree nodesDo:
[:n| | twin |
twin := them next.
(n isSameAs: twin) ifFalse:
[self halt]]

_,,,^..^,,,_
best, Eliot


Reply | Threaded
Open this post in threaded view
|

Re: generators

Nicolas Cellier
+1 nice!

Le mar. 23 juin 2020 à 20:23, Eliot Miranda <[hidden email]> a écrit :
Hi All,

    I juts had occasion to use generators in anger for the first time.  I was trying to fnd out why a clone of a prse tree was reporting that it was not the same tree.  Generators made enumerating all nodes in each tree together trivial:

| them |
them := Generator on: [:g| newTree nodesDo: [:n| g yield: n]].
parseTree nodesDo:
[:n| | twin |
twin := them next.
(n isSameAs: twin) ifFalse:
[self halt]]

My message though comes from noticing that generators yield their values via next, so why not use nextPut: instead of yield: ?

e.g. I think this is better:

| them |
them := Generator on: [:g| newTree nodesDo: [:n| g nextPut: n]].
parseTree nodesDo:
[:n| | twin |
twin := them next.
(n isSameAs: twin) ifFalse:
[self halt]]

_,,,^..^,,,_
best, Eliot



Reply | Threaded
Open this post in threaded view
|

Re: generators

Stéphane Rollandin
In reply to this post by Eliot Miranda-2
> My message though comes from noticing that generators yield their values
> via next, so why not use nextPut: instead of yield: ?
Agreed. +1

Stef

Reply | Threaded
Open this post in threaded view
|

Re: generators

Tobias Pape
In reply to this post by Nicolas Cellier

> On 23.06.2020, at 20:28, Nicolas Cellier <[hidden email]> wrote:
>
> +1 nice!

yeah, nice addition!
I'd suggest to keep #yield: around, tho.
I think it is kind-of tied to the Generator idea (viz. Python, JavaScript, C#)..

Best regards
        -Tobias

>
> Le mar. 23 juin 2020 à 20:23, Eliot Miranda <[hidden email]> a écrit :
> Hi All,
>
>     I juts had occasion to use generators in anger for the first time.  I was trying to fnd out why a clone of a prse tree was reporting that it was not the same tree.  Generators made enumerating all nodes in each tree together trivial:
>
> | them |
> them := Generator on: [:g| newTree nodesDo: [:n| g yield: n]].
> parseTree nodesDo:
> [:n| | twin |
> twin := them next.
> (n isSameAs: twin) ifFalse:
> [self halt]]
>
> My message though comes from noticing that generators yield their values via next, so why not use nextPut: instead of yield: ?
>
> e.g. I think this is better:
>
> | them |
> them := Generator on: [:g| newTree nodesDo: [:n| g nextPut: n]].
> parseTree nodesDo:
> [:n| | twin |
> twin := them next.
> (n isSameAs: twin) ifFalse:
> [self halt]]
>
> _,,,^..^,,,_
> best, Eliot
>
>