3 ms·
Lazy Lists in Python and C++
- nvictor 15y agoLazy title: the author gives examples in Haskell, Python, R, C++, and C++0x.
- RodgerTheGreat 15y agoI made a toy implementation of this sort of thing in Forth: http://pastebin.com/vHaU3jPW http://pastebin.com/vHaU3jPW This code isn't very well factored, but I think the results are pretty clean looking. With this code: : printPair ." ( " . ." , " . ." )" space ; 1 2 counter 0 5 range zip 1 2 counter 3 7 range zip chain ' printPair map You get this output: ( 0 , 1 ) ( 1 , 3 ) ( 2 , 5 ) ( 3 , 7 ) ( 4 , 9 ) ( 5 , 11 ) ( 3 , 1 ) ( 4 , 3 ) ( 5 , 5 ) ( 6 , 7 ) ( 7 , 9 )
- abecedarius 15y agoIn Python, I'd normally use a generator: the language and libraries support you in coding that way. Occasionally you run into the only-iterate-through-once restriction as a problem, and for that I stick a memoizing adaptor in.
- keveman 15y agoIs there an explanation of how Python yield is implemented?
- Luyt 15y agoYes. See http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt http://docs.python.org/reference/simple_stmts.html#grammar-t... , and the source code is pretty well commented: http://svn.python.org/view/python/trunk/Objects/genobject.c?view=markup http://svn.python.org/view/python/trunk/Objects/genobject.c?...