Home grown continuations

  • Thread starter Nathaniel Talbott
  • Start date
N

Nathaniel Talbott

DISCLAIMER: It has been said that continuations can cause brain damage... I
am not liable for any such damage if you read further.

I was thinking about closures, continuations, etc., and I suddenly had this
thought that I might be able to "roll my own" continuations using closures.
It doesn't seem to work, though. Can anyone tell me why?

def make_c
yield(proc{|*r| return r})
end

puts(make_c do |c|
puts "In continuation..."
c.call("Done")
puts "...oops"
end)

C:\ruby-projects>ruby t.rb
In continuation...
...oops
nil

It's obvious that calling return in a block doesn't do what I thought it
did... so what does it do?


Nathaniel

P.S. Yes, I know about callcc... I was just playing.

<:((><
 
Y

Yukihiro Matsumoto

Hi,

In message "Home grown continuations"

|I was thinking about closures, continuations, etc., and I suddenly had this
|thought that I might be able to "roll my own" continuations using closures.
|It doesn't seem to work, though. Can anyone tell me why?

Since 1.8, return within the closures created by proc or lambda, just
terminates the closure execution. If you create a closure by
Proc.new, it causes error, since call frame information is already
lost unlike callcc.

matz.
 
J

Jim Weirich

Since 1.8, return within the closures created by proc or lambda, just
terminates the closure execution.

Huh? I'm not sure I am understanding. What should the following
return?

def x
10.times { |n| return 1 if n == 5 }
2
end
p x

I would expect 1, but you seem to be saying 2.
If you create a closure by
Proc.new, it causes error, since call frame information is already
lost unlike callcc.

So Proc.new is different? What about the following code?

def y
p = Proc.new { |n| return 1 if n == 5 }
10.times(&p)
2
end

Again, I would expect 1, but you seem to be saying it will be an error.

BTW, both return 1 in an not-very-recent version of 1.8.0. (But that
might be different in the previews).
 
J

Jim Weirich

DISCLAIMER: It has been said that continuations can cause brain damage... I
am not liable for any such damage if you read further.

I was thinking about closures, continuations, etc., and I suddenly had this
thought that I might be able to "roll my own" continuations using closures.

Nathaniel, although I'm not doing something a bit different than what
you are attempting, you might still enjoy the following links ...

http://onestepback.org/index.cgi/Tech/Programming/Kata/KataTwoCps.rdoc
http://onestepback.org/index.cgi/Tech/Programming/Kata/KataTwoNoTail.rdoc
http://onestepback.org/index.cgi/Tech/Programming/Kata/KataTwoCallCC.rdoc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top