continuations

Coroutine vs Continuation vs Generator

三世轮回 提交于 2019-12-17 10:09:41
问题 What is the difference between a coroutine and a continuation and a generator ? 回答1: I'll start with generators, seeing as they're the simplest case. As @zvolkov mentioned, they're functions/objects that can be repeatedly called without returning, but when called will return (yield) a value and then suspend their execution. When they're called again, they will start up from where they last suspended execution and do their thing again. A generator is essentially a cut down (asymmetric)

Simple Async operation with Continuation scenario doesn't work on a WPF app

China☆狼群 提交于 2019-12-13 05:11:02
问题 I have a really simple operation on a WPF app in order to try out async operations. Here is my complete code: static int GetPrimes() { var query = from n in Enumerable.Range(3, 5000000).AsParallel() where Enumerable.Range(2, (int)Math.Sqrt(n)).All(i => n % i > 0) select n; return query.Count(); } private void button1_Click(object sender, RoutedEventArgs e) { Task<int> task = Task.Factory.StartNew( () => GetPrimes(), TaskCreationOptions.LongRunning ); task.ContinueWith(x => { label1.Content =

How to make fromList lazy in this dynamic programming example?

社会主义新天地 提交于 2019-12-11 13:37:29
问题 module Main where import System.Random import Data.Foldable import Control.Monad import qualified Data.Map as M import qualified Data.Vector as V import Debug.Trace import Data.Maybe import Data.Ord -- Represents the maximal integer. maxBound is no good because it overflows. -- Ideally should be something like a billion. maxi = 1000 candies :: V.Vector Int -> Int --M.Map (Int, Int) Int candies ar = ff [l (V.length ar - 1) x | x <- [0..maxi]] where go :: Int -> Int -> Int go _ 0 = maxi go 0 j

Setjmp/longjmp in Ruby’s Continuation

左心房为你撑大大i 提交于 2019-12-11 07:28:32
问题 I was wondering about this while digging through the code of cont.c in Ruby’s current version. The documentation of setjmp says that calling longjmp on the jmp_buf structure after the caller of setjmp returned is an error. But, it seems that Ruby does this happily and does not crash: https://github.com/ruby/ruby/blob/05f087c844f0e1e7cfe323edcf591de64069a289/cont.c#L522 https://github.com/ruby/ruby/blob/05f087c844f0e1e7cfe323edcf591de64069a289/cont.c#L775 (+ a couple more usages with fibers)

How to use Control.Monad.Cont in a recursive function?

天涯浪子 提交于 2019-12-11 05:34:43
问题 I was providing an answer to this question and an idea came to me to use Cont monad. I don't know Haskell enough to explain why this program doesn't work import Control.Monad.Cont fib1 n = runCont (slow n) id where slow 0 = return 0 slow 1 = return 1 slow n = do a <- slow (n - 1) b <- slow (n - 2) return a + b main = do putStrLn $ show $ fib1 10 Error - main.hs:10:18: error: • Occurs check: cannot construct the infinite type: a2 ~ m a2 • In the second argument of ‘(+)’, namely ‘b’ In a stmt

Need help with Continuations-Error “found cps expression in non-cps position”

六眼飞鱼酱① 提交于 2019-12-11 02:09:36
问题 I try building the following simple Generator using the Scala 2.8 Continuations-PlugIn. Where does the following error come from? None/None/Some((Unit,Unit)) GenTest.scala:8: error: found cps expression in non-cps position yieldValue(1) None/None/Some((Unit,Unit)) GenTest.scala:9: error: found cps expression in non-cps position yieldValue(2) None/None/Some((Unit,Unit)) GenTest.scala:10: error: found cps expression in non-cps position yieldValue(3) Code: import scala.util.continuations._

F#: In which memory area is the continuation stored: stack or heap?

元气小坏坏 提交于 2019-12-10 22:28:20
问题 Question It is possible to make every recursive function tail recursive by follow the continuation-passing style (CPS). As I understand it you put everything after the first recursive call into a function and hand it over to the same call. Therefore the recursive call is the last statement in the function and the compiler is able to do the tail call optimization. This means that the recursion is replaced by a loop. There are no additional stack frames consumed. The continuation is a function

Can the CPS-styled `call/cc` be written in terms of a hypothetical non-CPS-styled `cc'`?

牧云@^-^@ 提交于 2019-12-10 18:13:17
问题 In a language which supports continuation, e.g. Scheme, Ruby, and Haskell, suppose there is a function cc' which takes no argument and return the current continuation, so that the caller that obtains a continuation by calling cc' can then invoke the continuation anywhere and as often as it likes. cc' can be written in terms of the CPS-styled call/cc , by passing an identity function as an argument to call/cc . Conversely, can the CPS-styled call/cc be written in terms of the non-CPS-styled cc

How can I get this function to be tail-recursive?

被刻印的时光 ゝ 提交于 2019-12-10 17:43:04
问题 I'm still trying to implement 2-3 finger trees and I made good progress (repository). While doing some benchmarks I found out that my quite basic toList results in a StackOverflowException when the tree ist quite large. At first I saw an easy fix and made it tail-recursive. Unfortunately, it turned out that toList wasn't the culprit but viewr was: /// Return both the right-most element and the remaining tree (lazily). let rec viewr<'a> : FingerTree<'a> -> View<'a> = function | Empty -> Nil |

How to discard a delimited continuation from within multiple nested functions?

两盒软妹~` 提交于 2019-12-10 17:33:47
问题 I study delimited continuations and am currently playing with discarding them to obtain an effect similar to raising exceptions. Here is what causes me trouble: const structure = type => cons => { const f = (f, args) => ({["run" + type]: f, [Symbol.toStringTag]: type, [Symbol("args")]: args}); return cons(f); }; const Cont = structure("Cont") (Cont => f => Cont(f)); const runCont = tf => k => tf.runCont(k); const reset = tf => of(tf.runCont(id)); const shift = f => Cont(k => f(k).runCont(id))