Best introduction to ‘continuations’. Ever. Do not even try to understand this one.
Summarizing:
- Continuations are like bookmarks: "hey Joe, come here and start again from this point". The context in which you invoke the code after the continuation point is the context you are currently, not the context you had when you defined the continuation. It’s easy to see it with this code in Ruby:
arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ] hello_var = "hello1" puts(hello_var) callcc{|$cc|} message = arr.shift hello_var = "hello2" puts(message) puts(hello_var) $cc.call unless message =~ /Max/
The output is:
hello1 Freddie hello2 Herbie hello2 Ron hello2 Max hello2
- Closures give you the lexical context you had when you defined the closure, including values of variables.