|Perl: the Markov chain saw|
Re: (Perl6) Groking Continuations (iterators)by tye (Sage)
|on Apr 08, 2003 at 16:29 UTC||Need Help??|
You use continuations to turn call-backs into iterators.
Oh, you wanted more of an explanation than that? Hmm. Okay.
First, you need to understand why call-backs suck and iterators are better so read Re: Are you looking at XML processing the right way? (merge).
Now, why is a call-backs API easier to provide? Well, for once thing, you can use recursion. Take something like File::Find. Let's make one that is extremely simple:
and now let's turn it into an iterator:
Do you see the problem? Here I am N layers deep in recursion with lots of interesting stuff in N instances each of @files, $path, $subdir, and whatever my for loop uses to keep track of where it is, and I want to return! I not only want to return all the way up my N layers of recursion, but I want to be able to come back right where I left off with my whole call stack just the way I left it so I continue right where I left off.
Well that is what continuations let you do (so I've heard, I haven't really studied them so I could well be wrong, but if so, someone will point this out and I'll learn something).
You can certainly turn the above code into an iterator without continuations, but it means getting rid of the recursion by implementing your own stack and stuffing that into some context container that the caller has to give back to you some way (usually via OO).