When and how to use continuation passing style

后端 未结 3 1382
执笔经年
执笔经年 2021-02-07 12:51

I have been reading about the continuation passing style programming technique (C# 3.0 implementation).

Why/when it would be useful to use this technique?

How ca

3条回答
  •  梦如初夏
    2021-02-07 13:32

    To answer your last question, continuation passing style is not the same a currying. You curry when you create a function out of another function, by specifying one, or more of its parameters, thus getting a function with fewer parameters. Currying in a functional programming language, such as F#, and C# to an extent, allows you to treat all functions as being a function of one variable. If the said function has more than one parameter, then it can be viewed as having on parameter and returning another function with the remaining parameters. This is an example of currying in c#:

    public static class FuncExtensions
    {
            public static Func> Curry(this Func f)
            {
                return a => () => f(a);
            }
    }
    
    Func f = x => x + 1;
    
    Func curried = f.Curry()(1);
    

    Where the function curried will always return 2. There are of course, more enlightening uses of this.

    In regards to continuation passing style, in addition to the Wes Dyer blog linked to, look into F# async workflows, which are instances of continuations, or the continuation monad. You can try to use the term continuation monad to find some additional articles.

提交回复
热议问题