我相信延续是回调的特例。一个函数可以回调任意数量的函数,任意次数。例如:
var array = [1, 2, 3];
forEach(array, function (element, array, index) {
array[index] = 2 * element;
});
console.log(array);
function forEach(array, callback) {
var length = array.length;
for (var i = 0; i < length; i++)
callback(array[i], array, i);
}
但是,如果一个函数回调另一个函数作为它所做的最后一件事,那么第二个函数称为第一个函数的延续。例如:
var array = [1, 2, 3];
forEach(array, function (element, array, index) {
array[index] = 2 * element;
});
console.log(array);
function forEach(array, callback) {
var length = array.length;
// This is the last thing forEach does
// cont is a continuation of forEach
cont(0);
function cont(index) {
if (index < length) {
callback(array[index], array, index);
// This is the last thing cont does
// cont is a continuation of itself
cont(++index);
}
}
}
如果一个函数调用另一个函数作为它做的最后一件事,那么它被称为尾调用。像 Scheme 这样的一些语言会执行尾调用优化。这意味着尾调用不会产生函数调用的全部开销。相反,它被实现为一个简单的 goto(调用函数的堆栈帧被尾调用的堆栈帧替换)。
奖金:继续传球风格。考虑以下程序:
console.log(pythagoras(3, 4));
function pythagoras(x, y) {
return x * x + y * y;
}
现在,如果每个操作(包括加法、乘法等)都以函数的形式编写,那么我们将有:
console.log(pythagoras(3, 4));
function pythagoras(x, y) {
return add(square(x), square(y));
}
function square(x) {
return multiply(x, x);
}
function multiply(x, y) {
return x * y;
}
function add(x, y) {
return x + y;
}
此外,如果我们不允许返回任何值,那么我们将不得不使用延续,如下所示:
pythagoras(3, 4, console.log);
function pythagoras(x, y, cont) {
square(x, function (x_squared) {
square(y, function (y_squared) {
add(x_squared, y_squared, cont);
});
});
}
function square(x, cont) {
multiply(x, x, cont);
}
function multiply(x, y, cont) {
cont(x * y);
}
function add(x, y, cont) {
cont(x + y);
}
这种不允许返回值(因此您必须求助于传递延续)的编程风格称为延续传递风格。
然而,延续传递风格存在两个问题:
- 传递延续会增加调用堆栈的大小。除非您使用像 Scheme 这样消除尾调用的语言,否则您将面临堆栈空间不足的风险。
- 编写嵌套函数很痛苦。
第一个问题可以通过异步调用延续在 JavaScript 中轻松解决。通过异步调用延续,函数在调用延续之前返回。因此调用堆栈大小不会增加:
Function.prototype.async = async;
pythagoras.async(3, 4, console.log);
function pythagoras(x, y, cont) {
square.async(x, function (x_squared) {
square.async(y, function (y_squared) {
add.async(x_squared, y_squared, cont);
});
});
}
function square(x, cont) {
multiply.async(x, x, cont);
}
function multiply(x, y, cont) {
cont.async(x * y);
}
function add(x, y, cont) {
cont.async(x + y);
}
function async() {
setTimeout.bind(null, this, 0).apply(null, arguments);
}
第二个问题通常使用一个名为的函数来解决,该函数call-with-current-continuation
通常缩写为callcc
. 不幸的是callcc
不能在 JavaScript 中完全实现,但我们可以为它的大多数用例编写一个替换函数:
pythagoras(3, 4, console.log);
function pythagoras(x, y, cont) {
var x_squared = callcc(square.bind(null, x));
var y_squared = callcc(square.bind(null, y));
add(x_squared, y_squared, cont);
}
function square(x, cont) {
multiply(x, x, cont);
}
function multiply(x, y, cont) {
cont(x * y);
}
function add(x, y, cont) {
cont(x + y);
}
function callcc(f) {
var cc = function (x) {
cc = x;
};
f(cc);
return cc;
}
该callcc
函数接受一个函数f
并将其应用于current-continuation
(缩写为cc
)。current-continuation
是一个延续函数,它在调用callcc
.
考虑函数的主体pythagoras
:
var x_squared = callcc(square.bind(null, x));
var y_squared = callcc(square.bind(null, y));
add(x_squared, y_squared, cont);
第二current-continuation
个callcc
是:
function cc(y_squared) {
add(x_squared, y_squared, cont);
}
同样current-continuation
,第一个callcc
是:
function cc(x_squared) {
var y_squared = callcc(square.bind(null, y));
add(x_squared, y_squared, cont);
}
由于current-continuation
第一个callcc
包含另一个callcc
,因此必须将其转换为继续传递样式:
function cc(x_squared) {
square(y, function cc(y_squared) {
add(x_squared, y_squared, cont);
});
}
因此,从本质callcc
上讲,将整个函数体从逻辑上转换回我们的起点(并将这些匿名函数命名为cc
)。使用 callcc 实现的 pythagoras 函数变为:
function pythagoras(x, y, cont) {
callcc(function(cc) {
square(x, function (x_squared) {
square(y, function (y_squared) {
add(x_squared, y_squared, cont);
});
});
});
}
同样,您不能callcc
在 JavaScript 中实现,但您可以在 JavaScript 中实现延续传递样式,如下所示:
Function.prototype.async = async;
pythagoras.async(3, 4, console.log);
function pythagoras(x, y, cont) {
callcc.async(square.bind(null, x), function cc(x_squared) {
callcc.async(square.bind(null, y), function cc(y_squared) {
add.async(x_squared, y_squared, cont);
});
});
}
function square(x, cont) {
multiply.async(x, x, cont);
}
function multiply(x, y, cont) {
cont.async(x * y);
}
function add(x, y, cont) {
cont.async(x + y);
}
function async() {
setTimeout.bind(null, this, 0).apply(null, arguments);
}
function callcc(f, cc) {
f.async(cc);
}
该函数callcc
可用于实现复杂的控制流结构,例如 try-catch 块、协程、生成器、fibers等。