continuations

difference between closures and continuations

北城余情 提交于 2019-12-04 21:39:19
问题 Can someone please explain the difference between closures and continuations? The corresponding articles in wikipedia do not really compare the differences between the two. 回答1: A closure is a function that captures data from the environment on which it was declared. int myVar = 0; auto foo = [&] () { myVar++; }; <- This lambda forms a closure by capturing myVar foo(); assert(myVar == 1); A continuation is a more abstract concept, and refers to what code should be executed afterwards. It can

SICP, Continuation Passing Style and Clojure's trampoline

爷,独闯天下 提交于 2019-12-04 19:57:23
I am working with SICP and exercise 2.29-b gave me the opportunity to have fun with the Continuation Passing Style while traversing mobiles and branches. To make the story short, each mobile has left and right branch, which are composed by a length and either a numeric weight or another mobile. The question asks to find the total weight given a mobile. After the first mutually recursive solution, quite simple, I tried and successfully implemented a cps' one: (defn total-weight-cps [mobile] (letfn [(branch-weight-cps [branch kont] (let [structure (branch-structure branch)] (if (mobile? (branch

How to handle Jetty exception - a long running HTTP request times out, but the process it calls never terminates and Jetty is unhappy

為{幸葍}努か 提交于 2019-12-04 18:34:21
问题 I have a Jetty server handling long running HTTP requests- the responses are generated by an a different process X and end up in a collector hash which Jetty requests periodically check. There are 3 cases: Process X finishes before the timeout period of the HTTP request - no problem Process X finishes after the timeout period of the request - no problem Process X never finishes - below exception occurs How do I detect this situation (3) and prevent the exception while allowing the other two

Use MonadRef to implement MonadCont

淺唱寂寞╮ 提交于 2019-12-04 14:55:43
问题 There is a well known issue that we cannot use forall types in the Cont return type. However it should be OK to have the following definition: class Monad m => MonadCont' m where callCC' :: ((a -> forall b. m b) -> m a) -> m a shift :: (forall r.(a -> m r) -> m r) -> m a reset :: m a -> m a and then find an instance that makes sense. In this paper the author claimed that we can implement MonadFix on top of ContT r m providing that m implemented MonadFix and MonadRef . But I think if we do

How GOTO statement in Groovy?

主宰稳场 提交于 2019-12-04 13:06:15
I saw this nice blog post about a Scala continuations that 'emulates' a GOTO statement in the Scala language. (read more about Continuations here ) I would like to have the same in the programming language Groovy. I think it's possible within a Groovy compiler phase transformation . I'm working on an Domain-Specific Language (DSL), and preferred embedded in Groovy. I would like to have the GOTO statement, because the DSL is an unstructured language (and is generated from workflow diagrams). I need a 'labeled' goto statement, not to line numbers. The DSL is a language for workflow definitions,

Scala Continuations - Why can't my shifted call be inside a try-catch block?

允我心安 提交于 2019-12-04 10:28:17
I'm new to Scala continuations, and relatively new to the scala language in general. I tried playing with Scala continuations and wrote the following code: case class MyException(msg:String) extends Exception def go:Int = reset { println("enter your input") val my_check = //try { val user_input = readLine() if (!user_input.matches("\\w+")) { throw new MyException("illegal string: " + user_input) } shift { k: (Boolean => Int) => { if (user_input == "true") { k(true) } else if (user_input == "false") { k(false) } else { // don't even continue 0 } } } } // catch { // case MyException(msg) =>

Understanding Haskell callCC examples

帅比萌擦擦* 提交于 2019-12-04 08:24:08
I am having trouble understanding the answers to a previous question . I'm hoping that an explanation of the following will clarify things. The following example comes from fpcomplete import Control.Monad.Trans.Class import Control.Monad.Trans.Cont main = flip runContT return $ do lift $ putStrLn "alpha" (k, num) <- callCC $ \k -> let f x = k (f, x) in return (f, 0) lift $ putStrLn "beta" lift $ putStrLn "gamma" if num < 5 then k (num + 1) >> return () else lift $ print num The output is alpha beta gamma beta gamma beta gamma beta gamma beta gamma beta gamma 5 I think I understand how this

How does Smalltalk manipulate call stack frames (thisContext)?

若如初见. 提交于 2019-12-04 07:07:29
The Smalltalk object thisContext look strange and marvelous. I can't understand what it is and how it works. And even how it enables continuations. For C's call-stack, I can easily imagine how is it implemented and working. But for this... I can't. Please help me to understand it. I think it is not an easy question. The stack is reified in the image with instances of MethodContext. A MethodContext can have a sender, which is another MethodContext. That one can have another one...generating a whole stack. MethodContext are instantiated by the VM while executing CompiledMethod (which are also

Play's continuations holding http threads

半腔热情 提交于 2019-12-04 04:35:45
We have implemented a feature in our web app that updates the GUI in response to new events in the server by using Play's continuations, just like a chat app. After running it for some time in production we started to experience server hangs, more specifically the http connector of our Glassfish server stopped accepting new requests. A thread dump shows us that all http threads from the http thread pool are waiting for Play Promises to be invoked. Our thread pool has 5 threads (the default number) which means there are 5 clients waiting to be notified and the server cannot accept more http

Can a “Cont r a” post-process the result of its continuation

谁说我不能喝 提交于 2019-12-04 03:37:18
问题 the Cont r a type stands for a function which takes a continuation a->r and produces a result of type r . So both the continuation and the entire Cont r a produce a result of the same type r . My question is: are the two results necessarily the same value , or can a Cont r a post-process the result from the continuation and produce a different value, albeit of the same type r ? I tried using (+1) for post-processing (note the + 1 --<-- ): c1 :: Int -> Cont r Int c1 x = let y = 2*x in cont $