# Info on continuations / callcc?

**URL:** <https://www.ruby-forum.com/t/info-on-continuations-callcc/101902>\
**Category:** Ruby\
**Created:** [August 8, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902 "2006-08-08T16:48:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasudevram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bbce88/32.png) [@vasudevram](https://www.ruby-forum.com/u/vasudevram)\
**Post date:** [August 8, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902/1 "2006-08-08T16:48:01Z")

</div>

Hi,

I am Googling and will do more, found some stuff, but interested to get  
viewpoints of list members on:

Continuations in Ruby / the callcc keyword.

Saw a few URLs which had some info, some of which I understood. But  
like I said, personal viewpoints are good to have.

Thanks  
Vasudev

---

<div class="post-metadata">

**Author:** ![vasudevram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bbce88/32.png) [@vasudevram](https://www.ruby-forum.com/u/vasudevram)\
**Post date:** [August 8, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902/2 "2006-08-08T17:12:06Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > Saw a few URLs which had some info, some of which I understood. But
> 
> here’s some code that uses it to impliment external iterators in ruby, may be  
> helpful:
> 
> module Enumerable  
> class Iterator

Thanks, will look at it.

Vasudev

---

<div class="post-metadata">

**Author:** ![vasudevram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bbce88/32.png) [@vasudevram](https://www.ruby-forum.com/u/vasudevram)\
**Post date:** [August 8, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902/3 "2006-08-08T16:54:02Z")

</div>

On Tue, 8 Aug 2006, vasudevram wrote:

> ## Thanks Vasudev
> 
> ## Software consulting and training [http://www.dancingbison.com](http://www.dancingbison.com) PDF conversion toolkit: [Conversion of other file formats to PDF download | SourceForge.net](http://sourceforge.net/projects/xtopdf) (stable version 1.0, supports plain text and DBF conversions to PDF)

here’s some code that uses it to impliment external iterators in ruby,  
may be  
helpful:

module Enumerable  
class Iterator  
public  
attr\_reader :enumerable, :has\_next  
def initialize enumerable, meth = ‘each’, end\_value = nil,  
&end\_block  
@enumerable = enumerable  
@meth = meth  
@end\_value = end\_value  
@end\_block = end\_block  
initialize\_fetch\_block  
end  
def next  
ret = @has\_next ? fetch\_next\_element : fetch\_end\_value  
(ret.nil? ? ret : (ret.size == 1 ? ret.first : ret))  
end  
def rewind  
initialize\_fetch\_block  
self  
end  
protected  
def initialize\_fetch\_block  
callcc do |@after\_fetch|  
@has\_next = true  
@enumerable.send(@meth) do |\*@next\_element|  
callcc do |@next\_fetch| @after\_fetch.call end  
end  
@has\_next = false  
@next\_fetch = nil  
@after\_fetch.call  
end  
@after\_fetch = nil  
end  
def fetch\_next\_element  
result = @next\_element  
callcc do |@after\_fetch| @next\_fetch.call end  
@after\_fetch = nil  
result  
end  
def fetch\_end\_value  
@end\_block ? @end\_block.call : @end\_value  
end  
end

def iterator(\*a, &b); Iterator::new(self, \*a, &b); end

class \<\< self  
def each(\*enumerables, &block)  
iterators = enumerables.collect{|e| e.iterator}  
while true  
args = iterators.collect{|i| i.next}  
if args.detect{|arg| arg}  
block.call \*args  
else  
return enumerables  
end  
end  
end  
def collect(\*enumerables, &block)  
ret = []  
each(\*enumerables){|\*args| ret \<\< (block.call \*args)}  
ret  
end  
alias map collect  
def enumerate(enumerables, meth = ‘each’, \*a, &b)  
rets = []  
iterators = enumerables.map{|e| e.iterator(\*a, &b)}  
iterators.each\_with\_index do |iterator, idx|  
rets[idx] \<\< iterator.next  
end  
rets  
end  
end  
end

if $0 == **FILE**  
#{{{  
a = [‘fee’,‘fie’,‘foe’,‘fum’]  
h = {‘k’ =\> ‘v’, ‘K’ =\> ‘V’}

ai = a.iterator  
hi = h.iterator

while ((n = ai.next)) do  
puts n.inspect  
end

while ((n = hi.next)) do  
puts n.inspect  
end

Enumerable::each a, h do |elem, kv|  
puts elem.inspect  
puts kv.inspect  
end

p Enumerable::collect(a,h){|e,kv| [e,kv]}  
#}}}  
end

**END**

OUTPUT:

“fee”  
“fie”  
“foe”  
“fum”  
[“K”, “V”]  
[“k”, “v”]  
“fee”  
[“K”, “V”]  
“fie”  
[“k”, “v”]  
“foe”  
nil  
“fum”  
nil  
[[“fee”, [“K”, “V”]], [“fie”, [“k”, “v”]], [“foe”, nil], [“fum”, nil]]

-a

---

<div class="post-metadata">

**Author:** ![vasudevram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bbce88/32.png) [@vasudevram](https://www.ruby-forum.com/u/vasudevram)\
**Post date:** [August 8, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902/4 "2006-08-08T18:18:14Z")

</div>

Bil K. wrote:

> > Saw a few URLs which had some info, some of which I understood. But  
> > Later,  
> > –  
> > Bil  
> > [http://fun3d.larc.nasa.gov](http://fun3d.larc.nasa.gov)

No, actually I found some others.  
Thanks, will check the ones you mention  
Vasudev

---

<div class="post-metadata">

**Author:** ![vasudevram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bbce88/32.png) [@vasudevram](https://www.ruby-forum.com/u/vasudevram)\
**Post date:** [August 8, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902/5 "2006-08-08T17:36:09Z")

</div>

vasudevram wrote:

> Hi,

Hi.

> I am Googling and will do more, found some stuff, but interested to get  
> viewpoints of list members on:
> 
> Continuations in Ruby / the callcc keyword.
> 
> Saw a few URLs which had some info, some of which I understood. But  
> like I said, personal viewpoints are good to have.

Presumably you found,

[http://onestepback.org/index.cgi/Tech/Programming/Kata/KataTwoCallCC.rdoc](http://onestepback.org/index.cgi/Tech/Programming/Kata/KataTwoCallCC.rdoc)

You might also get a hold of the unit tests that  
Chad and Jim used during their RubyConf05 session  
to force one to wrestle with callcc.

Later,

---

<div class="post-metadata">

**Author:** ![vasudevram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bbce88/32.png) [@vasudevram](https://www.ruby-forum.com/u/vasudevram)\
**Post date:** [August 9, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902/6 "2006-08-09T16:52:50Z")

</div>

“vasudevram” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Saw a few URLs which had some info, some of which I understood. But  
> like I said, personal viewpoints are good to have.

Well, you probably ran across my old page from 2003 that does “amb” in  
ruby in a translated-straight-from-scheme way.

But for a more recent look at what I can use callcc for, decode my  
“de-optimised” solution to ruby quiz number 84:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/199337](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/199337)

That solution uses a few of the more obscure ruby tricks, such as  
singleton classes, the coerce and to\_s methods, the inject method,  
etc., in addition to using continuations in a manner that’s rather  
less civilized than nice structured co-routines or enumerators. To  
see what’s going on, you might replace this line:

```
(0 ... n).inject(n-1){ |q,p|

```

with

```
(0 ... n).inject(n-1){ |q,p| p [q,p]

```

Actually, going through all the solutions to quiz 84 and decoding them  
all will give probably more of a ruby education than you’d reasonably  
want. (I keep meaning to write up a full dissection of each entry - I  
really think that doing that, and arranging the entries in the proper  
order, would make for a nice ruby crash course)

My recent post to the list on “probability fun in ruby” also uses  
callcc, wrapped in a more rubyish version of amb I made after seeing  
how people implemented amb in quiz #70.

---

<div class="post-metadata">

**Author:** ![vasudevram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bbce88/32.png) [@vasudevram](https://www.ruby-forum.com/u/vasudevram)\
**Post date:** [August 9, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902/7 "2006-08-09T21:05:20Z")

</div>

“vasudevram” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Thanks 🙂 Sounds interesting - will check it out.  
> The URL  
> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/199337](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/199337)  
> doesn’t seem to be accessible. Will be Googling for any other locations

Yeah, there was a post to the list earlier today saying that  
blade.nagaokaut was going to be down for several days (until the 13th,  
I think). In any case, a google groups search for “something like  
this monstrosity” turns up the same post here:

[http://groups.google.com/group/ruby-talk-google/browse\_thread/thread/496a4af9dc4771bf/a730a3cd6014b502?lnk=st&q=&rnum=3#a730a3cd6014b502](http://groups.google.com/group/ruby-talk-google/browse_thread/thread/496a4af9dc4771bf/a730a3cd6014b502?lnk=st&q=&rnum=3#a730a3cd6014b502)

> meanwhile, interested to know why you say that decoding  
> just the solutions to quiz 84 “will give you more of a ruby education  
> than you’d reasonably want”? Not disputing the opinion, just interested  
> to know …

Quiz #84 was by a good margin the most popular ruby quiz both in terms  
of number of solutions and in terms of number of participants.  
Although several people just solved the problem, many others went to  
extreme lengths to optimize for memory used, for speed, or for code  
length. At least one solution (Kelly Norton’s) managed to do the  
whole thing in one (nasty) expression. (And then of course there was  
my solution, where I was trying to de-optimize for both speed and  
memory)

Unfortunately, the web archives of ruby-talk that most people point to  
are temporarily down. Fortunately, google groups has most of the  
solutions captured in the thread I linked to above.

---

<div class="post-metadata">

**Author:** ![vasudevram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bbce88/32.png) [@vasudevram](https://www.ruby-forum.com/u/vasudevram)\
**Post date:** [August 9, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902/8 "2006-08-09T20:26:18Z")

</div>

Daniel M. wrote:

> with  
> callcc, wrapped in a more rubyish version of amb I made after seeing  
> how people implemented amb in quiz #70.

Thanks 🙂 Sounds interesting - will check it out.  
The URL  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/199337](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/199337)  
doesn’t seem to be accessible. Will be Googling for any other locations  
for this quiz; meanwhile, interested to know why you say that decoding  
just the solutions to quiz 84 “will give you more of a ruby education  
than you’d reasonably want”? Not disputing the opinion, just interested  
to know …

Vasudev

> **[dancingbison.com](http://ww38.dancingbison.com/)**
>
> Learn Python Programming

---

<div class="post-metadata">

**Author:** ![vasudevram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bbce88/32.png) [@vasudevram](https://www.ruby-forum.com/u/vasudevram)\
**Post date:** [August 15, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/info-on-continuations-callcc/101902/9 "2006-08-15T14:12:34Z")

</div>

Daniel M. wrote:

> this monstrosity" turns up the same post here:  
> Although several people just solved the problem, many others went to  
> extreme lengths to optimize for memory used, for speed, or for code  
> length. At least one solution (Kelly Norton’s) managed to do the  
> whole thing in one (nasty) expression. (And then of course there was  
> my solution, where I was trying to de-optimize for both speed and  
> memory)
> 
> Unfortunately, the web archives of ruby-talk that most people point to  
> are temporarily down. Fortunately, google groups has most of the  
> solutions captured in the thread I linked to above.

Thanks for the reply.  
Yes, then that sounds like a good idea - to check out that quiz’s  
answers - should  
give some ideas of different ways of doing the same thing in Ruby.

Vasudev

> **[dancingbison.com](http://ww38.dancingbison.com/)**
>
> Learn Python Programming
