From: "Florian Groß" Date: 2003-08-09T05:14:00+09:00 Subject: Re: Why does Ruby have callcc? Moin! Ben Giddings wrote: > Do you have any suggestions on how to improve my Continuation add-ons, > or are you happy with just using callcc? I tried to make a simpler replacement to callcc and I ended up with this: def Continuation.create(*args, &block) cc = nil; result = callcc {|c| cc = c; nil} result ||= args return *[cc, *result] end I didn't want to call this Continuation.new because it can return multiple values. Though the following simple examples are working well I would be interested to know if the simpler syntax comes at the loss of functionality. 1) Word swapper: cc, first, second, called = Continuation.create("Hello", "World") puts first + " " + second cc.call(second, first, true) unless called ^D Hello World World Hello 2) Counter: cc, counter = Continuation.create(10) print counter, " " cc.call(counter - 1) if counter > 0 puts ^D 10 9 8 7 6 5 4 3 2 1 0 3) Inject: module Enumerable def new_inject(initial=nil) array = self.dup initial = array.shift if initial.nil? cc, state, array = Continuation.create(initial, array) state = yield(state, array.shift) cc.call(state, array) if array.length > 0 return state end end puts [1, 2, 3].new_inject {|state, item| state + item} ^D 6 Regards, Florian Gro�