nontinuations and throw / catch

Discussion in 'Ruby' started by treefrog, Feb 10, 2005.

  1. treefrog

    treefrog Guest

    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.
    treefrog, Feb 10, 2005
    #1
    1. Advertising

  2. treefrog

    Pit Capitain Guest

    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
    Pit Capitain, Feb 10, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri
    Replies:
    2
    Views:
    13,000
    Kevin Spencer
    Oct 27, 2003
  2. Andy Fish
    Replies:
    17
    Views:
    758
    Roedy Green
    Apr 19, 2004
  3. Matt
    Replies:
    1
    Views:
    333
    Andy Fish
    Jun 10, 2004
  4. Replies:
    15
    Views:
    7,495
    Roedy Green
    Sep 8, 2005
  5. Emanuele D'Arrigo

    To throw or to throw not?

    Emanuele D'Arrigo, Nov 14, 2008, in forum: Python
    Replies:
    6
    Views:
    312
    Emanuele D'Arrigo
    Nov 15, 2008
Loading...

Share This Page