nontinuations and throw / catch

T

treefrog

Hi folks,
I've been playing around with continuations a bit. What I'd like to be
abale to do is execute arbitary code sequences, suspend them, and then
resume them. All very well, and all what I can do using Continuation.

The problem is, that I want to be able to modify certain aspects of the
context while the code is suspended, and then have these picked up when
I resume. Consider the following code:

#callcc test

class Func
attr_reader :cont
def initialize
end

def do
@cont ? cont.call : do1
puts "after cont call"
end

def do1
puts 'A'
callcc { |cc| @cont=cc; throw :wait}
puts 'B'
@cont=nil
end


end

f=Func.new
(1..2).each do |i|
puts "Before do : i= #{i}"
catch :wait do
f.do
end
puts "After do : i= #{i}"


end



Now consider the output:

Before do : i= 1
A
After do : i= 1
Before do : i= 2
B
after cont call
After do : i= 1
Before do : i= 2
A
After do : i= 2

Basically, what is happening is that the continuation is the entire
programme context, and hence the loop variable i is overwritten.

This isn't quite what I am trying to achieve, which is to be able to
use a throw / catch mechanism in conjunction with the continuation to
be able to jump out of a function at arbitary points, and then return
to it later. What I really want is to be able to keep the contxt from
the catch downwards, so that I can keep the higher layers of the
context. In this case the desired output would be:

Before do : i= 1
A
After do : i= 1
Before do : i= 2
B
after cont call
After do : i= 2

Any ideas?

Best regards

Steve

PS (before anyone asks, I'm trying to put together a library for
discrete event simulation, and I'd like to be able to easily specify
arbitary breakpoints in functions, and then resume at them later, in
the knowledge that some (higher level) data may have changed.
 
P

Pit Capitain

Hi Steve,

you can find examples for continuations and coroutines in the archives of
ruby-talk. You can also look at the Generator class in the standard library. I
think for jumping back and forth you need two continuations instead of just one
continuation and a throw/catch. I can dig out some code samples if you need them.

Regards,
Pit
 

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

No members online now.

Forum statistics

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

Latest Threads

Top