Info on continuations / callcc?

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

[email protected]in.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

On Tue, 8 Aug 2006, vasudevram wrote:

Thanks
Vasudev

Software consulting and training
http://www.dancingbison.com
PDF conversion toolkit: 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

Bil K. wrote:

Saw a few URLs which had some info, some of which I understood. But
Later,

Bil
http://fun3d.larc.nasa.gov

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

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

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,

“vasudevram” [email protected] 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

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.

“vasudevram” [email protected] writes:

Thanks :slight_smile: Sounds interesting - will check it out.
The URL
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

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.

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 :slight_smile: Sounds interesting - will check it out.
The URL
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
http://www.dancingbison.com

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
www.dancingbison.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs