www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Iteration madness

reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
So I'm working away at getting Pyd to automatically wrap a class's 
operator overloads, and it's going great. Until, that is, I get to opApply.

Oh yeah.

Python does iterators differently. First, you call PyObject_GetIter on 
an object, which returns an iterator object. Then you just keep calling 
PyIter_Next on the iterator object until it returns null.

It turns out that wrapping opApply with this sort of interface is 
essentially impossible. Long story short, it requires static closures in D.

More specifically, it requires functionality equivalent to Python's 
"yield" keyword. This is a keyword that causes a function to act like a 
generator. As the generator is iterated over, it just keeps running the 
function, using the results of the yields as the iterator values. This 
is a classic Python way of creating an iterator for a class.

So, in the meantime, wrapped opApply functions will have to be more 
literally translated as functions with a callback argument.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki
Jul 03 2006
parent Rémy Mouëza <ray.jay.ay.moueza DoNtSpAm.gmail.com> writes:
In article <e8akf8$p17$1 digitaldaemon.com>, Kirk McDonald says...
So I'm working away at getting Pyd to automatically wrap a class's 
operator overloads, and it's going great. Until, that is, I get to opApply.

Oh yeah.

Python does iterators differently. First, you call PyObject_GetIter on 
an object, which returns an iterator object. Then you just keep calling 
PyIter_Next on the iterator object until it returns null.

It turns out that wrapping opApply with this sort of interface is 
essentially impossible. Long story short, it requires static closures in D.

More specifically, it requires functionality equivalent to Python's 
"yield" keyword. This is a keyword that causes a function to act like a 
generator. As the generator is iterated over, it just keeps running the 
function, using the results of the yields as the iterator values. This 
is a classic Python way of creating an iterator for a class.

So, in the meantime, wrapped opApply functions will have to be more 
literally translated as functions with a callback argument.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki
It seems that you need something like StackThread: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/39435 http://assertfalse.com From what I have understood, it implements coroutines, e.g. generator like objects for D. It seems to still have some issues, but should be usable.
Jul 03 2006