Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-04-2006, 09:01 AM   #1
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Continuations and the "yield" Keyword

So, as I understand it, continuations are a way of wrapping execution state up into an object (loosely speaking) that can later be invoked to restore that state, correct? I've never used continuations explicitly (like Scheme's call/cc) but I'm wondering if the "yield" keywords in Ruby and Python don't do the same thing. From what I've read online, Ruby supports continuations through Kernel#callcc, but normal Python does not provide this. "Python In A Nutshell" talks about iterators when discussing the "yield" keyword, but it seems to me like it's pretty much the same as a continuation. So what do others see in this? Do Ruby and Python both use continuations to facilitate the "yield" keyword? Or do they work differently?

And please correct me if I'm wrong anywhere. This seems like a powerful subject and I'm still trying to wrap my mind around it.
Old 07-04-2006, 12:30 PM   #2
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 7,912

Rep: Reputation: 2566Reputation: 2566Reputation: 2566Reputation: 2566Reputation: 2566Reputation: 2566Reputation: 2566Reputation: 2566Reputation: 2566Reputation: 2566Reputation: 2566
Here's one way to think of it...

Let's say that I need to define a function which iterates over all of (say) the files in a directory structure; or over a list that's so big and hairy and enormous that I really don't want to have to actually generate and store that list all at once so that I can then iterate over it.

So what I'm wanting to build is a function that, each time it is called, will return "the next" value to me .. the next filename in the directory, for example. Something like:
while (getNextFileName(&foo)) ...
(Note: I'm not trying to write in any particular language here... merely to illustrate the essential concept.)

Now, this necessarily implies that the function that I wish to write (getNextFileName()) must have, and must be able to preserve from call to call, some notion of its internal state. It has to know which filename it returned to me "last time" so that it can correctly deliver the next one "next time."

Ordinarily, I would have to do this with some kind of cumbersome "internal-state record" that the caller must use one function to create, then must hand to me with each call, and then must dispose of:
stateRec = beginSearch();
while (getNextFileName(&foo, staterec)) ...
It works. But it's clumsy. It obliges my caller to know something about how I work, and to obey the protocols correctly, or else I'll screw-up. This has also made every piece of code that uses my function be semantically dependent upon me. If I wanted or needed to redefine that protocol, then every single bit of code that uses it must be found and, at the very very least, recompiled. Also, the code within my function is harder to write because it's sort of "inside-out." This is not expressive of what I'd really like to be able to say here.

I'd really like to have a way for the language to implement this notion of "internal state" so that I can write my function something like this:
 int getNextFileName(char *filename) {
  <<start searching>>
  while <<there's another filename>> {
    <<assign the filename to the buffer provided>>
  <<clean up the search>>
Each time the function executes yield, it "returns to its caller" but doesn't discard its internal state. When the next call occurs, the function won't start over at the beginning: it will resume execution at the yield-point. This goes on, as many times as necessary, until the function finally executes return. And the caller does not (have to) know nor care just how I'm doing what I'm doing.) That can be a "big win."

This concept can be very useful if you want to, say, give the effect of "iterating through some huge and hairy list" but you don't actually want to store the list. You create a "stateful function" that algorithmically produces each value when it's needed.

This concept also restores the notion that "an object, or a unit of code, ought to be self-contained and subject to change without notice."



python, ruby

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
Support for "equalize" keyword for iproute!! vishamr2000 Linux - General 4 05-29-2006 12:34 AM
Which Databases in nsswitch.conf can be used with "dns" keyword saudoi Solaris / OpenSolaris 5 12-29-2005 10:37 AM
java variable scope - use of "this" keyword zeppelin147 Programming 1 11-21-2005 10:04 PM
exportfs: /etc/exports:1 unknown keyword "show" ukrainet Linux - Newbie 2 12-15-2004 07:18 AM

All times are GMT -5. The time now is 12:45 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration