Win a signed copy of Programming in Lua 3rd Edition
thread.gmane.org
thread.gmane.org
I've heard it compared it to "The Unix Programming Environment" in terms of being a classic, and I agree.
I find myself coming back to this book again and again, even though I haven't been able to justify Lua on any projects really. Just now the treatment of coroutines really helped with using Python coroutines (not generators).
It was my understanding that implementing "trampolining" as explained by http://dabeaz.com/coroutines/ in Python 2.5 is basically what "yield from" does in Python 3.3. Maybe "yield from" is more efficient. If anyone has any comments on that I'd be interested.
The PIL book has a few interesting examples of things that you can do with coroutines that are not possible with generators (plain yield). Also there is a paper by the Lua authors called "revisiting coroutines" -- although that was published in 2009, they don't acknowledge that Python has coroutines! They talk about Python's "limited" generators.
EDIT: Sorry, "yield from" is for generators. So coroutines are yield/send(). Generators are yield/yield from.
"yield from" is indeed the equivalent of coroutine trampolines, but for generators.
The right way to think about it is that coroutines are a push-based model while generators are a pull-based model. They don't mix, despite using the same keyword! If you want to mix them you would have to introduce a buffer between them or something. This is somewhat explained in Beazley's material.
And coroutines are more powerful than generators, as explained by the "revisiting coroutines" paper (and which should be obvious if you try to transcribe some coroutine patterns into generators.) Even after adding "yield from", which removed one restriction of generators, coroutines are still more powerful.
IIRC, the biggest difference between the languages is that Lua coroutines are "stackless" so you can have "non-generator" functions calling "generator" functions. In Python you need to use something like `yield from` and that turns the calling functions into generator functions as well.
But you can already the equivalent with Python coroutines (and Lua), using the trampolining technique in Beazley's material (and PEP 342).
Python coroutines aren't any less powerful than Lua coroutines. If you still believe they are I would be very interested in an example.
I won't bother asking what model is the better one. The Python version is more rigid but its also more explicit...
Builtin map() is for generators only. You can write map for coroutines as well:
def mapcoro(func, target):
while True:
value = (yield)
target.send(func(value))
(untested, I think you need to catch StopIteration around the yield line)You can do everything with Python coroutines that you can do with Lua coroutines. You could say that Python doesn't need generators, but that's a different argument. I agree it is confusing... I was confused for a long time.
I considered using Python, since I'm very familiar with the language, but in the end, the ease of embedding Lua in c/c++ sold it for me.
And for some reason I can't pin down, it's just fun to hack with.
But they are in portuguese :p