jastrachan | 17 Apr 06:00 2004

Re: Generator Expressions

On 17 Apr 2004, at 02:16, Mark C. Chu-Carroll wrote:
> On Apr 16, 2004, at 3:21 PM, Stepan Koltsov wrote:
>> Hi, all,
>> I've started implementing Generator Expression (not Simple 
>> Generators!),
>> http://jira.codehaus.org/secure/ViewIssue.jspa?key=GROOVY-54 .
>> Implementation of GE is trivial, unlike SG. And the implementations
>> have almost nothing in common.
> I'm a bit skeptical that.  Have you looked at Icon, which is (I think) 
> where Python got its
> generator notion from?
> You *can* do generator expressions as a special simple case of general 
> generators. But you can
> also implement general generators, and use them to implement generator 
> expressions.
> Generators are *really* amazing when you've got them in a form where 
> they really work. Back
> in the day, I used to do a lot of Icon programming, and you wind up 
> using generators *everywhere*. It's kind of like how we use closures 
> in Groovy today: once you get used to having the buggers around, you 
> keep finding more ways to use them.

In some ways we can do generator-like things using closures.

a python style list-generator

[ i * 2 for i in 1..10]

could be done via

(1..10).collect { return it * 2 }

Though if there's a predicate in there it gets a little longer

[ i * 2 for i in 1..10 if i != 4]

(1..10).findAll { it != 4 }.collect { return it * 2 }

I've never used Icon myself - do you have some examples of other ways 
of using generators? They do seem cool - though maybe we can get by 
with closures? Or to put that another way, do you have some great 
examples of how generators are neater/better than the closure 

> I'd really hate to see Groovy pick up just the weaker special-case of 
> generators. While it's a lot more work to do full general generators, 
> it's not *that* hard, and the end result is really worth it. (I'd be 
> willing to help out implementing it... I don't know nearly enough 
> about the groovy implementation to
> do it myself, but I've got more experience than I care to admit with 
> doing the kinds of things with threads that will be needed to make it 
> work.

Is the only way to implement full general generators to use threads? I 
was kinda thinking we could implement things like a yield statement 
using a 1-method-deep continuation of some kind to avoid needing to use 

> My main job is the Stellation project at Eclipse.org,
> and I wrote the communications layer, which is a lightweight pub/sub 
> system, on top of which I implemented client/server RPC services.  
> Making that work involves pretty much the same kind of thread 
> synchronization that would be needed to do generators.)

Incidentally I'm working on an open source pub/sub messaging system too 
:) (http://activemq.codehaus.org/).