Why there is no Continuation.new?

Hi gurus and nubys,

I’m trying to get a grip on continuations.
I can understand why we have
callcc &block

Reading some stuff on continuations from scheme’s documentation this
is quite clear.

Btw, I wonder what’s wrong with a more rubyish way to use
continuations.
the sample from ri :

arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
callcc{|$cc|}
puts(message = arr.shift)
$cc.call unless message =~ /Max/

To me this seem crying to become:
arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
cc= Continuation.new # optional &blk
puts(message = arr.shift)
cc.call unless message =~ /Max/

probably I’m just dumb and I can’t really get Continuations…

[gabriele renzi surrender_it@rc1.vip.ukl.yahoo.com, 2004-03-10 09.14 CET]
[…]

Btw, I wonder what’s wrong with a more rubyish way to use
continuations.
the sample from ri :

arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
callcc{|$cc|}
puts(message = arr.shift)
$cc.call unless message =~ /Max/

To me this seem crying to become:
arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
cc= Continuation.new # optional &blk
puts(message = arr.shift)
cc.call unless message =~ /Max/

Because callcc doesn’t return a continuation, but, first: the value of
the block, and second: the argument to #call.

Here is an example (I am not good with examples…):

arr=[:a, :b, :c, :d]
message = callcc {|$cc| arr.shift}

^-- this is the returning point

in the first invocation (callcc), it returns the value of the block

when we reach (2), it comes back here and returns :EOF

when we reach (3), it comes back here and returns the

argument to $cc.call() (arr.shift)

if message.nil?
$cc.call(:EOF) # (2) if we reach the end of the array,
# we go back and return this symbol instead
end
puts message
$cc.call(arr.shift) unless message == :EOF # (3) we go back until
# we get an :EOF

Good luck.

Hi,

···

In message “Why there is no Continuation.new ?” on 04/03/10, gabriele renzi surrender_it@rc1.vip.ukl.yahoo.com writes:

To me this seem crying to become:
arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
cc= Continuation.new # optional &blk
puts(message = arr.shift)
cc.call unless message =~ /Max/

There’s no consensus that where the continuation should point when
Continuation.new is called. You suppose the continuation to the
invocation of Continuation.new. JavaScript people suppose the other
way. Their Continuation.new return the continuation of the current
method, which may be more useful.

						matz.

gabriele renzi wrote:

To me this seem crying to become:
arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
cc= Continuation.new # optional &blk
puts(message = arr.shift)
cc.call unless message =~ /Max/

I think I proposed exactly the same thing in the thread starting here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78151

I wanted a Continuation.new and a Continuation.run. It made sense to me
then, but I don’t remember what the final consensus was – whether I was
sane, insane, stupid or smart.

shrug

Ben

gabriele renzi surrender_it@remove.yahoo.it wrote in message news:0fit40laostu79fvclc0sp0vb0179q47m2@4ax.com

Btw, I wonder what’s wrong with a more rubyish way to use
continuations.
the sample from ri :

arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
callcc{|$cc|}
puts(message = arr.shift)
$cc.call unless message =~ /Max/

To me this seem crying to become:
arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
cc= Continuation.new # optional &blk
puts(message = arr.shift)
cc.call unless message =~ /Max/

For what it’s worth, in Smalltalk I use the equivalent of

cc = Continuation.current

and

Continuation.current{|cc| … }

Avi

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

Because callcc doesn’t return a continuation, but, first: the value of
the block, and second: the argument to #call.

I understand hat callcc calls the block passing the continuation to
it, I did’nt meant to replace it.
I just asked why the process by witch |$cc| is initialized is not
available to us.

I just knew of scheme’s call/cc till some time ago, and I considered
that was the only way to handle continuations.
Than I found “new Continuation” in javascript, and I wondered if we
could have (on side with callcc ) something like what exist in
Rhino[1].

[1]
http://www.apache.org/~jefft/forrest/samples/wikirenderer-site/wiki/RhinoWithContinuations.html

Here is an example (I am not good with examples…):

every example is a good example to me :slight_smile:

···

il Wed, 10 Mar 2004 20:28:34 +0900, Carlos angus@quovadis.com.ar ha scritto::

[gabriele renzi surrender_it@rc1.vip.ukl.yahoo.com, 2004-03-10 13.34 CET]

I understand hat callcc calls the block passing the continuation to
it, I did’nt meant to replace it.
I just asked why the process by witch |$cc| is initialized is not
available to us.

I just knew of scheme’s call/cc till some time ago, and I considered
that was the only way to handle continuations.
Than I found “new Continuation” in javascript, and I wondered if we
could have (on side with callcc ) something like what exist in
Rhino[1].

But your example would not work with the Rhino version.
Continuation.new creates a continuation that returns from the
enclosing block. Since the enclosing block is the top level, when you
do $cc.call, it would exit the program.

I find the ruby version clearer. When you do callcc{|c| … }, it
means: when you’ll do c.call, it will return exactly to this point.

In the Rhino version, when you do c = Continuation.new, it means: when
you’ll do c.call, it will return from the enclosing
block/function/etc…

Your modified example from Pickaxe then would become something like:

arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
dummy = begin

^-- cc.call will return here

cc = Continuation.new
end
puts(message = arr.shift)
cc.call unless message =~ /Max/

And what happens with Continuation.new inside iterators? cc.call
should return like next or like break?

def loop
while true
dummy = yield

^-(1)

end
end

dummy = loop do

^-(2)

cc = Continuation.new
break
end
puts(message = arr.shift)
cc.call unless message =~ /Max/ # ← returns to (1) (next) or (2) (break)?

Bah… it’s all more complicated… better stay with the ruby version
:slight_smile: