From: Dan Debertin Date: 2003-08-06T23:02:33+09:00 Subject: Re: Why does Ruby have callcc? Jim Bob writes: > So, how'd it get there? Is there some killer use for callcc that I'm > missing? Was it just for the "gee whiz" factor? I can't explain the "how'd it get there" part; I'll leave that to Matz and friends. Continuations are great for any situation in which you not only need to jump out of a context (multiple nested loops, etc.), but also get back in to the same context you left. You can't do that with catch/throw. A good example of this is co-routines -- you need to save your state in the first function, start the second one, then jump back to where you were in the first, and so on. This example runs two iterators simultaneously: # Iterate simultaneously from a to b, and from b to a def updown(a, b) iter1, iter2 = ( a < b ? [a.method(:upto), b.method(:downto)] : [a.method(:downto), b.method(:upto)] ) f, cc1, cc2 = nil iter1.call(b) do |o| callcc do |cc1| f = o cc2.call if(cc2) iter2.call(a) do |i| callcc do |cc2| yield f, i cc1.call if(cc1) end end end end end Of course, one can't discount the considerable "Gee wiz" factor of continuations ;-). Dan -- /^Dan Debertin$/ | airboss@nodewarrior.org | www.nodewarrior.org |