延续,以及为什么它们会导致回调意大利面
在回调中编写会迫使您在某个时候以类似于“连续传递样式”(CPS)的方式编写,这是一种非常强大但难度很大的技术。它代表了控制的完全反转,实际上是“颠倒”了计算。CPS 使您的代码结构明确地反映程序的控制流(有时是好事,有时是坏事)。实际上,您是在显式写下匿名函数的堆栈。
作为理解此答案的先决条件,您可能会发现这很有用:
http://matt.might.net/articles/by-example-continuation-passing-style/
例如,这就是您正在做的事情:
function thrice(x, ret) {
ret(x*3)
}
function twice(y, ret) {
ret(y*2)
}
function plus(x,y, ret) {
ret(x+y)
}
function threeXPlusTwoY(x,y, ret) {
// STEP#1
thrice(x, // Take the result of thrice(x)...
function(r1) { // ...and call that r1.
// STEP#2
twice(y, // Take the result of twice(y)...
function(r2) { // ...and call that r2.
// STEP#3
plus(r1,r2, // Take r1+r2...
ret // ...then do what we were going to do.
)
}
)
}
)
}
threeXPlusTwoY(5,1, alert); //17
正如您所抱怨的那样,这使得代码缩进相当,因为闭包是捕获此堆栈的自然方式。
拯救单子
取消缩进 CPS 的一种方法是像在 Haskell 中那样“单调地”编写。我们将如何做到这一点?在 javascript 中实现 monad 的一种好方法是使用点链表示法,类似于 jQuery。(有关有趣的转移,请参阅http://importantshock.wordpress.com/2009/01/18/jquery-is-a-monad/。)或者我们可以使用反射。
但首先我们需要一种“写下管道”的方法,然后我们可以找到一种方法将其抽象掉。可悲的是,在 javascript 中编写通用的 monad 语法有点困难,所以我将使用列表来表示计算。
// switching this up a bit:
// it's now 3x+2x so we have a diamond-shaped dependency graph
// OUR NEW CODE
var _x = 0;
var steps = [
[0, function(ret){ret(5)},[]], //step0:
[1, thrice,[_x]], //step1: thrice(x)
[2, twice,[_x]], //step2: twice(x)
[3, plus,[1, 2]] //step3: steps[1]+steps[2] *
]
threeXPlusTwoX = generateComputation(steps);
//*this may be left ambiguous, but in this case we will choose steps1 then step2
// via the order in the array
这有点丑 但是我们可以让这个 UNINDENTED “代码” 工作。我们可以担心稍后让它更漂亮(在最后一节中)。在这里,我们的目的是写下所有“必要的信息”。我们想要一种简单的方法来编写每个“行”,以及我们可以将它们写入的上下文。
现在我们实现 a generateComputation
,它生成一些嵌套的匿名函数,如果我们执行它,这些函数将按顺序执行上述步骤。这是这样一个实现的样子:
function generateComputation(steps) {
/*
* Convert {{steps}} object into a function(ret),
* which when called will perform the steps in order.
* This function will call ret(_) on the results of the last step.
*/
function computation(ret) {
var stepResults = [];
var nestedFunctions = steps.reduceRight(
function(laterFuture, step) {
var i = step[0]; // e.g. step #3
var stepFunction = step[1]; // e.g. func: plus
var stepArgs = step[2]; // e.g. args: 1,2
console.log(i, laterFuture);
return function(returned) {
if (i>0)
stepResults.push(returned);
var evalledStepArgs = stepArgs.map(function(s){return stepResults[s]});
console.log({i:i, returned:returned, stepResults:stepResults, evalledStepArgs:evalledStepArgs, stepFunction:stepFunction});
stepFunction.apply(this, evalledStepArgs.concat(laterFuture));
}
},
ret
);
nestedFunctions();
}
return computation;
}
示范:
threeXPlusTwoX = generateComputation(steps)(alert); // alerts 25
旁注:reduceRight
语义意味着右边的步骤将更深入地嵌套在函数中(将来会更深)。仅供那些不熟悉的人参考[1,2,3].reduce(f(_,_), x) --> f(f(f(0,1), 2), 3)
,和reduceRight
(由于糟糕的设计考虑)实际上相当于[1.2.3].reversed().reduce(...)
上面,generateComputation
制作了一堆嵌套函数,将它们相互包装起来准备,当用 求值时...(alert)
,将它们一个接一个地拆开以供计算。
旁注:我们必须使用 hack,因为在前面的示例中,我们使用闭包和变量名来实现 CPS。Javascript 不允许足够的反射来做到这一点,而不诉诸于制作一个字符串并eval
对其进行 ing (ick),因此我们暂时避开函数式样式并选择改变一个跟踪所有参数的对象。因此,上面更接近地复制了以下内容:
var x = 5;
function _x(ret) {
ret(x);
}
function thrice(x, ret) {
ret(x*3)
}
function twice(y, ret) {
ret(y*2)
}
function plus(x,y, ret) {
ret(x+y)
}
function threeXPlusTwoY(x,y, ret) {
results = []
_x(
return function(x) {
results[0] = x;
thrice(x, // Take the result of thrice(x)...
function(r1) { // ...and call that r1.
results[1] = r1;
twice(y, // Take the result of twice(y)...
function(r2) { // ...and call that r2.
results[2] = r2;
plus(results[1],results[2], // Take r1+r2...
ret // ...then do what we were going to do.
)
}
)
}
)
}
)
}
理想语法
但是我们仍然希望以一种理智的方式编写函数。理想情况下,我们希望如何编写代码以利用 CPS,同时保持我们的理智?文献中有很多内容(例如,Scalashift
和reset
运算符只是这样做的众多方法之一),但为了理智起见,让我们找到一种方法来为常规 CPS 制作语法糖。有一些可能的方法来做到这一点:
// "bad"
var _x = 0;
var steps = [
[0, function(ret){ret(5)},[]], //step0:
[1, thrice,[_x]], //step1: thrice(x)
[2, twice,[_x]], //step2: twice(x)
[3, plus,[1, 2]] //step3: steps[1]+steps[2] *
]
threeXPlusTwoX = generateComputation(steps);
……变成……
- 如果回调在一个链中,我们可以轻松地将一个输入到下一个,而无需担心命名。这些函数只有一个参数:回调参数。(如果没有,您可以在最后一行将函数 curry 如下。)这里我们可以使用 jQuery 样式的点链。
// SYNTAX WITH A SIMPLE CHAIN
// ((2*X) + 2)
twiceXPlusTwo = callbackChain()
.then(prompt)
.then(twice)
.then(function(returned){return plus(returned,2)}); //curried
twiceXPlusTwo(alert);
如果回调形成一个依赖树,我们也可以使用 jQuery 样式的点链,但这会破坏为 CPS 创建一元语法的目的,即扁平化嵌套函数。因此,我们不会在这里详细介绍。
如果回调形成依赖无环图(例如,2*x+3*x
,其中 x 被使用两次),我们将需要一种方法来命名一些回调的中间结果。这就是有趣的地方。我们的目标是尝试模仿http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style的语法,其do
-notation 将函数“解包”和“重新包装”进出 CPS。不幸的是,[1, thrice,[_x]]
语法是我们可以轻松达到的最接近的语法(甚至没有接近)。您可以用另一种语言编写代码并编译为 javascript,或者使用 eval(排队不祥的音乐)。有点矫枉过正。替代方案必须使用字符串,例如:
// SUPER-NICE SYNTAX
// (3X + 2X)
thriceXPlusTwiceX = CPS({
leftPart: thrice('x'),
rightPart: twice('x'),
result: plus('leftPart', 'rightPart')
})
generateComputation
您只需对我描述的内容进行一些调整即可做到这一点。首先,您将其调整为使用逻辑名称('leftPart'
等)而不是数字。然后你让你的函数实际上是惰性对象,其行为如下:
thrice(x).toListForm() == [<real thrice function>, ['x']]
or
thrice(x).toCPS()(5, alert) // alerts 15
or
thrice.toNonCPS()(5) == 15
(您可以使用某种装饰器以自动方式执行此操作,而不是手动执行。)
旁注:所有回调函数都应该遵循关于回调参数所在位置的相同协议。例如,如果您的函数以开头myFunction(callback, arg0, arg1, ...)
或者myFunction(arg0, arg1, ..., callback)
它们可能不是很兼容,但如果它们可能不是,您可以执行 javascript 反射黑客来查看函数的源代码并将其正则表达式,因此不必担心它。
为什么要经历这么多麻烦?这允许您混合setTimeout
s 和prompt
s 以及 ajax 请求,而不会遭受“缩进地狱”的困扰。您还可以获得一大堆其他好处(比如能够编写一个 10 行的非确定性搜索数独求解器,并实现任意控制流运算符),我不会在这里讨论。