[boo]
generator methods... looking good!
[ bamboo ] 17:24, Tuesday, 5 October 2004

Tough weekend but I've finally got generator methods to work.

Integrating generator methods with generator expressions with closures was a good challenge... Next! :)

And before I go I shall leave you with this amazing piece of cra... hmm... code:


b = 5
g = def ():
    a = 0
    yield { return a }
    yield { return ++a }
    yield { a = b; return { return (a+b)*i for i in range(3) } }
    assert 5 == a
    
    
for f in g():
    item = f()  
    if item isa callable:
        b = 3
        print(join(cast(callable, item)(), ", "))
    else:
        print(item)
which prints:
0
1
0, 8, 16
Understand why and you'll understand boo's main features.

TrackBack
Comments

Here's a slight modification that has "g" yielding an object instead of a callable - this modification highlights that boo infers the type of "g" based on what it returns, and also shows a cast "(f as callable)" which took me awhile to figure out how to do!

Unfortunately all indentation is lost in this comment :-(

b = 5
g = def ():
a = 0
yield a
yield { return a }
yield { return ++a }
yield { a = b; return { return (a+b)*i for i in range(3) } }
assert 5 == a

for f in g():
if f isa callable:
item = (f as callable)()
else:
item = f
if item isa callable:
b = 3
print(join(cast(callable, item)(), ", "))
else:
print(item)

--Bill Wood, October 10, 2004 07:10 PM
Post a comment









Remember personal info?