« Rails Studio Early Registration ends soon | Main | Shipping the Rails Book »

January 20, 2009

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c41c69e2010536e91acb970c

Listed below are links to weblogs that reference So I need a symetric coroutine example:

Comments

gabriele renzi

the dining philosophers problem ?
I'm not sure i understand, but if by symmetric you mean full coroutines (which yield and resume) a fairly good example is a state machine. The problem is finding a case where you need to stop the computation to wait for the next input.

In that case, IRB (or other REPLs) represents a good example: you have a state machine to parse ruby statements, move to a new state, yield a new prompt and wait for the next token to decide what sub-action to take next.

But I'm probably misunderstanding :)

Dave Thomas

Dining philosophers is really about resource management: I looked at it, and it doesn't really work. State machines are good, but I struggled to find something that fits into the constraints of a half page and which also makes sense.

erik

What about the prisoners dilemma? You could implement the tit for tat strategy or any other.

Jörg W Mittag

@gabriele: I think you have them backwards: a *symmetric* coroutine is a coroutine which has *one* function to yield control to another coroutine. An *asymmetric* coroutine is a coroutine which has *two* functions, one for *suspending* the coroutine and another for *resuming* another suspended coroutine.

Asymmetric coroutines are also sometimes called *semi-coroutines*, because their asymmetry means they aren't really "co". However, that is an unfortunate name, because semi-coroutine also has a different meaning: it means a coroutine which can only suspend when its call stack is empty, i.e. it can only suspend from within its body but not from within another function. This kind of crippled coroutine is also sometimes called a *generator* (Python, C#, JavaScript).

Jörg W Mittag

For the state machine idea: what about implementing a simple regular expression? Something like this: https://Gist.GitHub.Com/49866/ ?

Seth Ladd

Just a quick head's up. The code samples from Gist don't appear in Google Reader. Other than that, excited for the new Pick Axe!

Jim Weirich

Same fringe problem? I solved it once using continuations to emulate coroutines, so it should be easy to convert to a fiber example.

See http://onestepback.org/articles/same_fringe/index.html

Ben Orenstein

I'm seeing the same thing as Seth (no gist in Google Reader). Also, could someone point out where the 1.9-specific array portion is? All that code seems like it works under 1.8.5 to me. Thanks

pete

@ben:

Arrays don't have product and shuffle in pre-1.9 ruby, unless you're using something like facets.

Robert Dober

Dave,
I did not even know that Ruby had symmetric coroutines.
Maybe it would be helpful if you shared some of your code for better understanding of the problem?
Cheers
Robert

Chuck Vose

Token passing seems like a great example. Quick hackery led me to this example:
http://pastie.org/372299

It's not a great example, I don't know what to write to educate people, that's your area. :)

I couldn't think of meaningful work for the machines to do but it would be interesting in encryption or image processing I think. Each machine with it's own encryption key doing a set amount of cycles before passing on to the next machine.

Robert Dober

Ah I seem to see, symmetric coroutines in Ruby are just using a subset of the API, transferring control amongst themselves. This is a new concept to me. (obviously Lua influence bite me)
I somehow cannot stop of thinking about continuations, setting up fibers to transfer to other fibers. Could we do something like Scheme's amb?
I keep thinking about this, but wanted to share the idea because I am sure there are brighter people than me here ( not many of course ;).

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now available
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.

Site Search

  • Google Search

    The web
    PragDave