Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Continuation - where does it continue

Reply
Thread Tools

Continuation - where does it continue

 
 
michelemendel@gmail.com
Guest
Posts: n/a
 
      09-21-2005
Why does the following code print the line "doing other stuff" twice?


def cc
puts "do A"
puts "----> 1. #{caller}"
callcc { |cont| return cont}
puts "----> 2. #{caller}"
puts "do B"
end

puts "calling cc"
cont = cc()
puts "doing other stuff"
cont.call() if cont
puts "end of work"
puts


OUTPUT:
calling cc
do A
----> 1. remove2.rb:59
doing other stuff
----> 2. remove2.rb:59
do B
doing other stuff
end of work

 
Reply With Quote
 
 
 
 
Patrick Roemer
Guest
Posts: n/a
 
      09-21-2005
Responding to :

> Why does the following code print the line "doing other stuff" twice?
>
> def cc
> puts "do A"
> puts "----> 1. #{caller}"
> callcc { |cont| return cont}
> puts "----> 2. #{caller}"
> puts "do B"
> end
>
> puts "calling cc"
> cont = cc()
> puts "doing other stuff"
> cont.call() if cont
> puts "end of work"
> puts


The continuation 'continues' from exactly the state where it left off:
It starts after the callcc call, after returning from the cc method it
continues one 'stack frame'[1] deeper after the cc() call, just as in
normal execution - it won't 'jump back' to the code position after the
continuation call.

Best regards,
Patrick

[1] Java lingo, don't know what it's called in Ruby
 
Reply With Quote
 
 
 
 
Pit Capitain
Guest
Posts: n/a
 
      09-21-2005
schrieb:
> Why does the following code print the line "doing other stuff" twice?
>=20
> def cc
> puts "do A"
> puts "----> 1. #{caller}"
> callcc { |cont| return cont}


The continuation continues here, after the call to callcc.

> puts "----> 2. #{caller}" =20
> puts "do B"
> end
>=20
> puts "calling cc"
> cont =3D cc() # <------ (X)
> puts "doing other stuff"
> cont.call() if cont
> puts "end of work"
> puts
>=20
>=20
> OUTPUT:
> calling cc
> do A
> ----> 1. remove2.rb:59
> doing other stuff
> ----> 2. remove2.rb:59
> do B
> doing other stuff
> end of work


Above I've shown the place where the continuation continues. So when you=20
call the continuation, you're back in the cc method after the callcc.=20
The program prints the remaining messages of the cc method and returns=20
nil (the result of puts). After the cc method returns, you're back at=20
the place where the method has been called, which the line marked with=20
(X). cont is set to nil, and the program prints "doing other stuff" for=20
the second time.

If you dont' want this, you have to change the code to something like

puts "calling cc"
cont =3D cc()
if cont
puts "doing other stuff"
cont.call()
end
puts "end of work"
puts

Florian Gro=DF has written something to make Continuation handling easier=
=20
You should be able to find it in the mailing list archives.

Regards,
Pit


 
Reply With Quote
 
Jim Weirich
Guest
Posts: n/a
 
      09-21-2005

said:
> Why does the following code print the line "doing other stuff" twice?
>
>

01 def cc
02 puts "do A"
03 puts "----> 1. #{caller}"
04 callcc { |cont| return cont}
05 puts "----> 2. #{caller}"
06 puts "do B"
07 end
>

08 puts "calling cc"
09 cont =3D cc()
10 puts "doing other stuff"
11 cont.call() if cont
12 puts "end of work"
13 puts

This is the seqence of events (by line number). Comments on what's
happening are in parenthesis.

08: calling cc
09 (calling cc)
02: do A
03: ----> 1. remove2.rb:59
04: (returning the continuation)
10: doing other stuff
11: (calling the continuation, so picking up in the middle of cc)
05: ----> 2. remove2.rb:59
06: do B
07: (returning from cc to the original location cc call site.
Now we return a nil value)
10: doing other stuff
11: (since cc returned a nil the second time, we don't call the
continuation this time)
12: end of work

--=20
-- Jim Weirich http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)



 
Reply With Quote
 
michele
Guest
Posts: n/a
 
      09-21-2005
AHA - EUREKA!!!

Thanks for your very quick answers! After "studying" continuations for
a couple of days, reading everthing I could find about, I think I
finally understand it. Your answers made it all clear.


Michele

 
Reply With Quote
 
Randy Kramer
Guest
Posts: n/a
 
      09-21-2005
On Wednesday 21 September 2005 10:36 am, Jim Weirich wrote:
> said:
> > Why does the following code print the line "doing other stuff" twice?


> This is the seqence of events (by line number). Comments on what's
> happening are in parenthesis.


Thanks, the line numbers help a lot!

regards,
Randy Kramer


 
Reply With Quote
 
Sean O'Halpin
Guest
Posts: n/a
 
      09-22-2005
Hi,

this finally cleared up Ruby's continuations for me too.

Would this be the effect you're after?
# code
def cc
puts "do A"
puts "----> 1. #{caller}"
cont =3D callcc { |cont| return cont}
puts "----> 2. #{caller}"
puts "do B"
end

puts "calling cc"
if cont =3D cc()
puts "doing other stuff"
cont.call() if cont
puts "end of work"
puts
end

#output
__END__
calling cc
do A
----> 1. continuations.rb:10
doing other stuff
----> 2. continuations.rb:10
do B
#end

I'm curious - what problem are you trying to solve?

Regards,
Sean



On 9/21/05, michele <> wrote:
> AHA - EUREKA!!!
>
> Thanks for your very quick answers! After "studying" continuations for
> a couple of days, reading everthing I could find about, I think I
> finally understand it. Your answers made it all clear.
>
>
> Michele
>
>
>



 
Reply With Quote
 
rpardee@gmail.com
Guest
Posts: n/a
 
      09-22-2005
What I don't understand is why the line:

04 callcc { |cont| return cont}

Doesn't give an 'undefined local method or variable "callcc"' error.
To my noob eyes that looks like a method call w/an accompanying block.
Have you got a clue for me?

Thanks!

-Roy

 
Reply With Quote
 
Jim Weirich
Guest
Posts: n/a
 
      09-22-2005
On Wednesday 21 September 2005 10:51 pm, wrote:
> What I don't understand is why the line:
>
> 04 callcc { |cont| return cont}
>
> Doesn't give an 'undefined local method or variable "callcc"' error.
> To my noob eyes that looks like a method call w/an accompanying block.


You've got good eyes. That's exactly what it is ... a call to the "callcc"
method passing a block.

--
-- Jim Weirich http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)


 
Reply With Quote
 
michele
Guest
Posts: n/a
 
      09-22-2005
I justed wanted to understand how continuations work and where it
happens, so I made the simplest example possible I could think of. I
wanted to start work one place, leave it in the middle, do some other
work, and then continue from where I left.

Your answer as well as Pit Capitain's above solved the problem.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WHY DOES FIREFOX CONTINUE TO DO THIS???? Prisoner at War Firefox 0 08-25-2008 12:43 PM
does not contain debug information. Press OK to Continue Karthik C Programming 15 06-24-2008 08:33 AM
debug 'continue' does not appear to work right newsposter@cox.net Python 3 12-13-2005 04:39 PM
If a mailto is sent the page does not continue to load McGoo@virgin.net Javascript 2 09-09-2003 02:05 PM
why window.open script not firing? just postback...continuation of previous post KathyB ASP .Net 2 07-17-2003 02:21 PM



Advertisments
 



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