[Stackless] Stackless support generator functions?

Richard Cooper Richard.Cooper at aprsmartlogik.com
Mon Sep 30 14:16:45 CEST 2002

Quite correct, good example. If you need to pass in a "how to output"
function the you can't work with yield.

def function4(x):
      yield x

wouldn't work for (approximately) the same reasons

def function5(x):
      return x

wouldn't. Functions 1-3 are getting their results out through a globally
scoped 'thing' (stdout, channel1 and List) where as yield and return get
results out through the return value. Both are perfectly valid. It's just a
question of preferred style and using the clearest code for the job in

Richard "We're both right, woohoo!" Cooper

* Discovering the clearest code for the job in hand is left as a exercise
for the reader ;-)

-----Original Message-----
From: Aaron Watters [mailto:aaron at reportlab.com]
Sent: 26 September 2002 20:38
To: Richard Cooper
Subject: Re: [Stackless] Stackless support generator functions?

You are right.  Thanks!

I guess I chose a bad example, but I think my general point
still stands.  Your correction to my example is well taken,

Ok, how about this

def inorderMap(tree, function):
    (value, children) = tree
    for c in children:
        inorderMap(c, function)

You can do this

def function1(x):
      print x

def function2(x):

def function3(x):

inorderMap(tree, function1); inorderMap(tree, function2); 
inorderMap(tree, function3)

but this does NOT work (no?)

def function4(x):
      yield x

Basically in my view the "yield" mechanism and procedural
abstraction don't really mix well.

As always corrections are always appreciated, thanks.

  -- Aaron Watters

Richard Cooper wrote:

>Actually a yield version of your preorderToChannel function wouldn't be
>more complicated. Yes, you can only yield up one level but you can quite
>easily yield what was just yielded to you.
>Of the top of my head it would look like this (Untested):
>def preorderGenerator(tree):
>    (node, children) = tree
>    yield node
>    for child in children:
>        for x in preorderGenerator(child):
>            yield x
>Richard Cooper

Stackless mailing list
Stackless at www.tismer.com

More information about the Stackless mailing list