Questions tagged [continuations]

In computer science and programming, a continuation is an abstract representation of the control state. A continuation reifies an instance of a computational process at a given point in the process's execution. It contains information such as the process's current stack (including all data whose lifetime is within the process e.g. "local variables"), as well the process's point in the computation.

Using continuation, when a function calls another function, that is the last thing it does. An extra argument is part of every function, which will be used to pass each function's continuation. Instead of waiting for the called function to return, it puts any work it wants to do afterwards into a continuation, which it passes to the function. Essentially, this involves breaking up the code into a collection of callback functions.

535 questions
15
votes
1 answer

Continuing a statement on the next line WITH A COMMENT

If I have a statement in Ruby that I want to continue on the next line, normally I would add a backslash at the end of the line like this: print x \ + y But if I have comments on the line, it doesn't work: print x #show x + y # show y Is there a…
Bryan Locke
  • 2,337
  • 5
  • 25
  • 30
15
votes
4 answers

generator/block to iterator/stream conversion

Basically I want to convert this: def data(block: T => Unit) to a Stream (dataToStream is a hypothetical function that do this conversion): val dataStream: Stream[T] = dataToStream(data) I suppose this problem could be resolved by…
Dawid Grzesiak
  • 402
  • 4
  • 9
15
votes
4 answers

Correct terminology for continuations

I've been poking around continuations recently, and I got confused about the correct terminology. Here Gabriel Gonzalez says: A Haskell continuation has the following type: newtype Cont r a = Cont { runCont :: (a -> r) -> r } i.e. the whole (a…
Martin Drautzburg
  • 5,143
  • 1
  • 27
  • 39
14
votes
2 answers

"call-cc" patterns in Scala?

I found a good article, about call with current continuation patterns. As I understand, they use Scheme and undelimited continuations. Can the patterns from the article be implemented in Scala? Is there any article about delimited continuations…
Michael
  • 10,185
  • 12
  • 59
  • 110
14
votes
2 answers

Can call-with-current-continuation be implemented only with lambdas and closures?

Does anyone know if call/cc can be implemented with just lambdas and closures? It seems that call/cc interrupts the program's flow (like an exception) but lambdas and closures can't do that. Therefore I think call/cc can't be implemented via lambdas…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
13
votes
2 answers

How do I schedule a conditional ContinueWith

I have some GUI on a bunch of LINQ queries. The queries take some time to execute, so I would like for the GUI to be responsive and show busyindicators and progress bars. Many of the queries are to check for certain conditions existing in the data.…
Tormod
  • 4,551
  • 2
  • 28
  • 50
13
votes
4 answers

Is there a fast language that supports portable continuations?

I'm looking for a fast language (ie. a language that can be compiled natively to achieve performance not more than 3 or 4 times slower than C), which supports portable continuations. By this I mean a continuation that can be serialized on one…
sanity
  • 35,347
  • 40
  • 135
  • 226
13
votes
1 answer

Jumping forward with the continuation monad

It is possible to jump backward in a program with the continuation monad: {-# LANGUAGE RecursiveDo #-} import Control.Monad.Fix import Control.Monad.Trans.Cont setjmp = callCC (\c -> return (fix c)) backward = do l <- setjmp -- some code to…
13
votes
3 answers

How to split and dispatch an async control-flow using Continuations?

I have an asynchronous control-flow like the following: ActorA ! DoA(dataA, callback1, callbackOnErrorA) def callback1() = { ... ActorB ! DoB(dataB, callback2, callbackOnErrorB) } def callback2() = { ActorC ! DoC(dataC, callback3,…
hotzen
  • 2,800
  • 1
  • 28
  • 42
12
votes
2 answers

Converting Scala @suspendable Method into a Future

suppose I have a sleep function: def sleep(delay:Int) : Unit @suspendable = { .... } is it possible to have a function future that creates an async version of the sleep function that can be awaited on synchronously. def future(targetFunc: (Int =>…
benmmurphy
  • 2,503
  • 1
  • 20
  • 30
12
votes
2 answers

Difference between delimited and undelimited continuations

I guess the difference between delimited and undelimited continuations is like the difference between call and jump. If we invoke delimited continuation it will return to the caller once it finishes. If we invoke undelimited continuation it works…
12
votes
1 answer

Does "reset" require "shift" inside the block?

Is it correct that reset requires shift inside the block? I tried it and got the following: scala> reset {} error: cannot cps-transform expression (): type arguments [Unit,Unit,Nothing] do not conform to method shiftUnit's type parameter bounds…
Michael
  • 10,185
  • 12
  • 59
  • 110
12
votes
3 answers

Is there a way to chain functions like withCString?

Is there a way to chain functions like withCString? By that I mean any function that looks something like f :: Foo -> (CFoo -> IO a) -> IO a. For example, lets say there is a function cFunc :: CString -> CFoo -> CBar -> IO () Usualy, I would do…
12
votes
1 answer

How does this continuation-passing style Clojure function generator work?

This is from the Joy of Clojure, 2nd Edition. http://www.manning.com/fogus2/ (defn mk-cps [accept? kend kont] (fn [n] ((fn [n k] (let [cont (fn [v] (k ((partial kont v) n)))] (if (accept? n) (k 1) …
mparaz
  • 2,049
  • 3
  • 28
  • 47
12
votes
4 answers

Continuations in Ruby

Has anyone ever done work to get Ruby to do continuations (like Seaside on Smalltalk)?
Jeff Waltzer
  • 532
  • 5
  • 12