LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-04-2006, 09:01 AM   #1
taylor_venable
Member
 
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
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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:
Code:
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:
Code:
stateRec = beginSearch();
while (getNextFileName(&foo, staterec)) ...
endSearch(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:
Code:
 int getNextFileName(char *filename) {
  <<start searching>>
  while <<there's another filename>> {
    <<assign the filename to the buffer provided>>
    yield(True);
  }
  <<clean up the search>>
  return(False);
}
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."

HTH...
 
  


Reply

Tags
python, ruby



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:55 PM.

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