标签: continuations

理解 call-with-continuation 的实现

我试图理解用 python 代码编写的方案过程:

def callcc(proc):
    "Call proc with current continuation; escape only"
    ball = RuntimeWarning("Sorry, can't continue this continuation any longer.")
    def throw(retval): ball.retval = retval; raise ball
    try:
        return proc(throw)
    except RuntimeWarning as w:
        if w is ball: return ball.retval
        else: raise w
Run Code Online (Sandbox Code Playgroud)

它来自本教程:http : //norvig.com/lispy2.html

以上是如何工作的?是什么ball意思,为什么 a proc(edure?) 被称为 athrow作为其参数值?评论“仅转义”是什么意思?


顺便说一句,这是我目前(可能是被误导的)对适用于 python 的延续的理解,这类似于传递一个带产量的函数:

def c(func, *args, **kwargs):
    # func must be a coroutine
    return func(*args, **kwargs)

def inc(x=0):
    while True:
        yield …
Run Code Online (Sandbox Code Playgroud)

lisp python scheme continuations callcc

1
推荐指数
1
解决办法
127
查看次数

是否有用于重写CPS的宏?

例如,我有两个异步方法

(get-a 10 (lambda (a) (get-b a (lambda (b) (display b)))
Run Code Online (Sandbox Code Playgroud)

但我想写一些类似的东西

(define (a (get-a 10)))
(define (b (get-b a)))
(display b)
Run Code Online (Sandbox Code Playgroud)

scheme continuations functional-programming callcc continuation-passing

0
推荐指数
1
解决办法
140
查看次数

是否继续记录PC并注册状态?

目前,当我正在尝试功能语言的延续时,我的理解是继续记录当前程序计数器和寄存器文件,当返回延续时,PC和注册文件将恢复为它记录的值.

所以在Might博客文章中的以下愚蠢的例子中,

; right-now : -> moment
(define (right-now)
  (call-with-current-continuation 
   (lambda (cc) 
     (cc cc))))

; go-when : moment -> ...
(define (go-when then)
  (then then))  


; An infinite loop:
(let ((the-beginning (right-now)))
  (display "Hello, world!")
  (newline)
  (go-when the-beginning))  ; here the-beginning continuation passed to go-when, which ultimately will have an continuation applied to an continuation, that returns a continuation, which will cause the the program point resumed to the PC and registers states recorded in it.
Run Code Online (Sandbox Code Playgroud)

我不确定我的理解是对的..如果你认为不是,请纠正我.....

scheme continuations functional-programming racket

0
推荐指数
1
解决办法
192
查看次数

使用Haskell将factorial写为命令函数

我在Haskell中编写了以下代码:

import Data.IORef
import Control.Monad
import Control.Monad.Trans.Cont
import Control.Monad.IO.Class
fac n = do
          i<-newIORef 1
          f<-newIORef 1
          replicateM_ n $ do
            ri<-readIORef i
            modifyIORef f (\x->x*ri)
            modifyIORef i (+1)
          readIORef f
Run Code Online (Sandbox Code Playgroud)

这是非常好的代码,它将factorial实现为命令式函数.但是replicateM_无法完全模拟真实for循环的使用.所以我尝试使用continuation创建一些东西,但我在这里失败的是我的代码:

ff = (`runContT` id) $ do
       callCC $ \exit1 -> do
         liftIO $ do
           i<-newIORef 1
           f<-newIORef 1
         callCC $ \exit2 -> do
           liftIO $ do 
             ri<-readIORef i
             modifyIORef (\x->x*ri)
             modifyIORef i (+1)
             rri<-readIORef i
             when (rri<=n) $ exit2(())
         liftIO $ do
           rf<-readIORef f
           return rf
Run Code Online (Sandbox Code Playgroud)

你能帮我纠正我的代码吗?谢谢

continuations haskell monad-transformers

0
推荐指数
1
解决办法
351
查看次数

制作Monad类的延续monad包装器实例

我的类型Foo是简单的包装Cont a a.我想使Foo类型成为类的实例Monad.我试试这个:

import Control.Monad.Cont

newtype Foo a = Foo {unFoo :: Cont a a}

instance Monad Foo where
    return = Foo . return
    Foo inner >>= func = Foo (inner >>= newFunc)
        where newFunc x = (unFoo $ func x)
Run Code Online (Sandbox Code Playgroud)

但我得到了这个错误:

Couldn't match type `a' with `b'
  `a' is a rigid type variable bound by
      the type signature for >>= :: Foo a -> (a -> Foo b) -> Foo b …
Run Code Online (Sandbox Code Playgroud)

monads continuations haskell class instance

0
推荐指数
1
解决办法
99
查看次数

多重延续链:boost.future 展开。怎么做

boost.future<T>在 boost 1.56 中使用了 continuation。

我有一个返回未来的 API,我想从延续内部使用它。所以理论上,我需要.unwrap在链接第二个延续之前的未来。

所以我执行以下操作:

auto result = api_returns_future().then([](boost::future<R> && result) {
                    do_something_with_result();

                    //Note I could call .get() here, but I don't
                    return api_returns_future();
              }).unwrap() //Now I unwrap the future here
              .then([](boost::future<R> && result) { ... });
Run Code Online (Sandbox Code Playgroud)

也就是说,我有:

  1. future<R>::then
  2. 在第一个延续中,我使用返回 a 的 API 调用从 lambda 返回boost::future<R>,然后我解开它。
  3. 之后我想附加另一个延续,但从未调用过这个延续。

题:

  1. 在第一个延续中这样做是正确的:(return api_returns_future().get()注意我.get()直接从延续内部调用`并放弃解包?。这个替代方案是否对我的代码的异步性有一些缺点?

编辑:我更新了问题以更好地反映经过更多研究后我想问的问题。

谢谢

c++ concurrency continuations boost c++11

0
推荐指数
1
解决办法
1729
查看次数

事件触发时完成任务

这应该很简单,但我只是无法将其聚焦。

在这个方法中

public static async Task<string> UnloadAsync(Assembly assy, bool silentFail = false)
{
  if (AssyLoadContext.__alcd.ContainsKey(assy))
  {
    var assemblyName = __namd.Where(kvp => kvp.Value == assy).First().Key;
    __alcd[assy].Unloading += alc => //signal the task to complete and return assemblyName
    __namd.Remove(assemblyName);
    __alcd[assy].Unload();
    __alcd.Remove(assy);
    Debug.WriteLine($"Unloaded assembly '{assy.GetName().Name}'");
  }
  if (silentFail) 
  {
    return null;
  }
  else
  {
    throw new InvalidOperationException($"Assembly '{assy.GetName().Name}' cannot be unloaded. Did you load it using AssyLoadContext.LoadWithPrivateContext(string assyPath)?");
  }
}
Run Code Online (Sandbox Code Playgroud)

AssemblyLoadContext.Unload()操作实际上是异步的,但不可等待。一旦不再有强 GC 引用等操作完成,程序集将卸载并触发 Unloading 事件。

返回值是assemblyName我想提供给延续的。

我可以找到所有关于需求的废话,await因为这就是产量发生的地方,但我不能那样写。你如何做到这一点没有 …

c# continuations task

0
推荐指数
1
解决办法
44
查看次数