From: Florian Gross Date: 2004-08-21T07:55:49+09:00 Subject: Re: ruby, actors, continuations, Kernel#callcc --------------080201020302010204060603 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit zuzu wrote: > but i still can't quite seem to grok continuations, at least in terms > of interpreting it for ruby, w/r/t closures and Kernel#callcc. this > after reading dan "parrot" sugalski's weblog I find that Continuations are easy to work with once you've got the confusing stuff wrapped away. I've attached Continuation.create which allows you to do such things: cc, counter = Continuation.create(10) puts counter cc.call(counter - 1) if counter > 0 Or a custom version of Enumerable#inject: (Won't work with Enumerables containing nil values and is way less efficient than the built-in one.) module Enumerable def cc_inject(initial_state = nil, &block) obj = self.to_a.clone initial_state = obj.shift if initial_state.nil? cc, state, item = Continuation.create(initial_state, obj.shift) unless item.nil? state = yield(state, item) cc.call(state, obj.shift) end return state end end Can't say anything about the whole actor thing, but I hope I was at least of some help. :) Regards, Florian Gross --------------080201020302010204060603 Content-Type: text/plain; name="simplecc.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="simplecc.rb" def Continuation.create(*args, &block) args = [args] if not args.nil? and not args.is_a? Array # 1.6.8 compatibility cc = nil; result = callcc {|c| cc = c; block.call(cc) if block and args.empty?} result ||= args return *[cc, *result] end --------------080201020302010204060603--