A problem with generators

bearophile bearophileHUGS at lycos.com
Wed Mar 17 15:24:36 PDT 2010


Lutger:
> Hi, is this generator thing really the same as opApply? I thought this is  
> more like co-routines.

In Python it's syntax for co-routines, but the syntax I have suggested for D is just sugar for the opApply.


>Can yield be as efficient as current opApply? (assuming inlining will eventually happen where possible)<

Currently opApply is not efficient with dmd. But LDC is able to often inline there, so I am happy. The yield as I mean it is as (in)efficient as the opApply.

Note that where you need more performance you can reverse inside-out the iterator, and use the range iterator methods. If they are not virtual dmd has no problem in inlining them.

And note that in 80-90% of your code you don't need 100% performance. There are many cases in your code where you need something that's handy, short, easy to remember and use, not error-prone, but you don't need to save few bits of time.


> yield may be easy to understand once you understand it :)

It contains more syntax sugar, so a system programmer can find it harder to understand what actually it's doing. This is a disadvantage. Too much syntax sugar gives syntax cancer :-)


It's prettier for 
> sure. But take a glimpse at this page on stackoverflow, the length of the 
> answers should tell you that it's not as straightforward as you might think 
> and that it's not only about the syntax:

I don't fully like the yield syntax of Python because they have first invented it for a simple but very useful purpose, and then they have badly extended it for coroutines too.
So now you have a statement that's used almost as a variable name. Ugh :-P

Regarding the semantics of python yield, it's not as simple as it was in the beginning, because they have bolted on it the coroutine semantics. And it contains a trap. That's probably why that thread on stack overflow is so long. This problem is not shared by my simpler yield I have suggested probably more than one year ago for D1.

Do you think future D newbies will will less hard to learn the opApply compared to the yield?

Anyway, the purpose of this post was not to discuss about yield syntax. The main problem with generators was not a problem about syntax, but to discuss the problem of computational complexity of nested generators. The links about C# show it very well.

Bye,
bearophile



More information about the Digitalmars-d mailing list