[英]How do i apply TCO(Tail Call Optimiztion) on recursive anonymous Function in ES5
If I can tco a named recursive function there should be a way to tco anonymous recursive function.If there is a way please explain how to do this below is my recursive function and TCO function.如果我可以 tco 命名递归函数,那么应该有一种方法可以 tco 匿名递归函数。如果有办法请解释如何做到这一点,下面是我的递归函数和 TCO 函数。
function recursive(length, callback) {
tco((function (i, sum) {
var args = arguments;
if (i > length) {
console.log("break statement");
callback(sum)
return sum
} else {
return args.callee(i + 1, sum + i)
}
}))(0, 0)
}
function tco(f) {
var value;
var active = false;
var accumulated = [];
return function accumulator() {
accumulated.push(arguments);
if (!active) {
active = true;
while (accumulated.length) {
value = f.apply(this, accumulated.shift());
}
active = false;
return value;
}
}
}
ES6 proposes changes to the tail-call system, an engine optimization. ES6 提出了对尾调用系统的更改,这是一项引擎优化。 A tail-call is when a function is called as the last statement in another function, like this:
尾调用是指一个函数作为另一个函数中的最后一条语句被调用,如下所示:
function doSomething() {
return doSomethingElse(); // tail call
}
ECMAScript 6 seeks to reduce the size of the call stack for certain tail calls in strict mode. ECMAScript 6 试图在严格模式下减少某些尾调用的调用堆栈的大小。 With this optimization, instead of creating a new stack frame for a tail call, the current stack frame is cleared and reused so long as the following conditions are met :
通过这种优化,只要满足以下条件,就不会为尾调用创建新的堆栈帧,而是清除并重用当前堆栈帧:
Perhaps the hardest situation to avoid is in using closures.
也许最难避免的情况是使用闭包。 Because a closure has access to variables in the containing scope, tail call optimization may be turned off.
因为闭包可以访问包含范围内的变量,所以可能会关闭尾调用优化。 For example:
例如:
"use strict";
function doSomething() {
var num = 1,
func = () => num;
// not optimized - function is a closure
return func();
}
Consider this function, which computes factorials:考虑这个计算阶乘的函数:
"use strict";
function factorial(n) {
if (n <= 1) {
return 1;
} else {
// not optimized - must multiply after returning
return n * factorial(n - 1);
}
}
In order to optimize the function, you need to ensure that the multiplication doesn't happen after the last function call.为了优化函数,您需要确保在最后一次函数调用之后不会发生乘法运算。
"use strict";
function factorial(n, p = 1) {
if (n <= 1) {
return 1 * p;
} else {
let result = n * p;
// optimized
return factorial(n - 1, result);
}
}
Source: An Awesome book by Nicholas Zakas, Understanding ECMAScript 6.来源: Nicholas Zakas 的一本很棒的书,Understanding ECMAScript 6。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.