From: Florian Gross Date: 2004-03-11T06:24:38+09:00 Subject: Re: Why there is no Continuation.new ? gabriele renzi wrote: > Hi gurus and nubys, Moin! > Btw, I wonder what's wrong with a more rubyish way to use > continuations. Nothing, it's just that we can't really use Continuation.new because standardized behavior would be to return only a single object: The newly created Continuation instance. (But that's quite useless.) However I've got this code in a file named simplecc.rb: def Continuation.create(*args, &block) cc = nil; result = callcc do |c| cc = c; block.call(cc) if block and args.empty? end result ||= args return *[cc, *result] end It's used like this: # This example counts from 10 down to 0 using continuations cc, counter = Continuation.create(10) puts counter cc.call(counter - 1) if counter > 0 # This implements inject using continuations # Please note that this implementation isn't fully compatible with the # original inject. (In order to keep it simple) class Array def cc_inject(value = nil) copy = self.clone cc, result, item = Continuation.create(value, nil) next_item = copy.shift if result and item cc.call(yield(result, item), next_item) elsif next_item cc.call(next_item, result) end return result end end [1, 2, 3].cc_inject { |state, item| state + item } # => 6 Regards, Florian Gross