continuations and clone problem

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default continuations and clone problem

    This class should convert internal-iterators to external-iterators.
    But how do I clone them?

    Any advice are deeply appreciated.

    --
    Simon Strandgaard




    server> ruby a.rb
    a.rb:28:in `next': undefined method `call' for false:FalseClass (NoMethodError)
    from a.rb:28:in `callcc'
    from a.rb:28:in `next'
    from a.rb:43
    server> expand -t2 a.rb
    class Object
    def deep_clone
    Marshal::load(Marshal.dump(self))
    end
    end

    class IteratorContinuation
    def initialize(instance, symbol)
    @instance = instance
    @symbol = symbol
    first
    end
    def first
    @value = nil
    @resume_where = false
    @return_where = Proc.new{}
    @instance.method(@symbol).call {|i|
    @value = i
    callcc{|@resume_where|
    @return_where.call
    return
    }
    }
    @resume_where = false
    @return_where.call
    end
    def next
    callcc{|@return_where| @resume_where.call }
    end
    def is_done?
    @resume_where == false
    end
    def current
    @value
    end
    end

    i1 = IteratorContinuation.new("hello world", :each_byte)
    i1.next
    i1.next
    i1.next
    i2 = i1.clone # deep_clone not possible
    i2.next
    p i1.current
    p i2.current
    server>
    Simon Strandgaard Guest

  2. Similar Questions and Discussions

    1. Clone stamp problem
      My clone stamp is not working properly. When I Alt/click it looks like it's working but when I stamp it's a dark color, not what I used as a sample....
    2. Clone problem
      Hi, I've got some problem when cloning a model and a separate animation into a third 3d member... In fact here's what I've got: -file A with...
    3. Clone problem, samples from "+" instead of my mouse cursor
      Using PE 2.0, Win XP I already reset prefernces with cntrl alt shift. I tried cloning with aligned off and on and it doesn't matter. I get...
    4. Home grown continuations
      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...
    5. Throwing exceptions into continuations.
      Hi all. I'm playing around with Ruby because I like experimenting with concurrency systems, and found out it has continuations. I'm going to be...
  3. #2

    Default Re: continuations and clone problem

    >>>>> "S" == Simon Strandgaard <neoneye@adslhome.dk> writes:

    Try this (not tested)

    S> class IteratorContinuation

    def marshal_dump
    [@instance, @symbol]
    end

    def marshal_load(arr)
    initialize(*arr)
    end

    S> end

    S> i1 = IteratorContinuation.new("hello world", :each_byte)
    S> i1.next
    S> i1.next
    S> i1.next
    S> i2 = i1.clone # deep_clone not possible

    i2 = i1.deep_clone

    S> i2.next
    S> p i1.current
    S> p i2.current



    Guy Decoux




    ts Guest

  4. #3

    Default Re: continuations and clone problem

    On Fri, 21 Nov 2003 19:53:47 +0900, ts wrote:
    >
    > Try this (not tested)
    >
    > def marshal_dump
    > def marshal_load(arr)
    Above partially solves it. However adding a position count, then it fully
    works. Thanks.


    Cloning continuations seems not to be possible ?

    irb(main):001:0> x = false
    => false
    irb(main):002:0> callcc{|x| }
    => nil
    irb(main):003:0> p x
    #<Continuation:0x81cba80>
    => nil
    irb(main):004:0> y = x.clone
    NoMethodError: allocator undefined for Continuation
    from (irb):4:in `clone'
    from (irb):4



    Therefore when doing cloning, I must iterate until @position are reached.
    Any idea if this skip-until-position code can be reduced?

    --
    Simon Strandgaard


    server> ruby a.rb
    "l"
    "o"
    server> expand -t2 a.rb
    class Object
    def deep_clone
    Marshal::load(Marshal.dump(self))
    end
    end

    class IteratorContinuation
    def initialize(instance, symbol, position=nil)
    @instance = instance
    @symbol = symbol
    @position = position || 0
    first
    @position.times { self.next } # TODO: can this be avoided ?
    end

    def first
    @value = nil
    @resume_where = false
    @return_where = Proc.new{}
    @instance.method(@symbol).call {|i|
    @value = i
    callcc{|@resume_where|
    @return_where.call
    return
    }
    }
    @resume_where = false
    @return_where.call
    end
    def next
    @position += 1
    callcc{|@return_where| @resume_where.call }
    end
    def is_done?
    @resume_where == false
    end
    def current
    @value
    end
    def clone
    self.class.new(@instance, @symbol, @position)
    end
    def marshal_dump
    [@instance, @symbol, @position]
    end
    def marshal_load(arr)
    initialize(*arr)
    end
    end

    i1 = IteratorContinuation.new("hello world", :each_byte)
    i1.next
    i1.next
    i1.next
    i2 = i1.clone
    #i2 = i1.deep_clone
    i2.next
    p i1.current.chr #=> "l"
    p i2.current.chr #=> "o"
    server>
    Simon Strandgaard Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139