尾递归优化流程解析

尾递归优化流程解析

function tco(f) {
  var value;
  var active = false;
  var accumulated = [];

  return function accumulator() {
    accumulated.push(arguments);
    if (!active) {

      active = true;
      while (accumulated.length) {
      	// 用apply单纯是为了传入类数组,不用拆分
      	//等同于 var shifted = accumulated.shift();  value = f(...shifted); 
       	value = f.apply(this, accumulated.shift())
        //让顶层对象运用传进来的sum 方法,参数为,value一直到最后输出之前都是undefined
      }

      active = false;
      return value;
    }
  };
}

var sum = tco(function(x, y) {
  if (y > 0) {
    return sum(x + 1, y - 1)
  }
  else {
    return x
  }
});
//tco返回一个accumulator函数,并通过这种方式将f传给accumulator


console.log(sum(1, 100)) //sum(1,100)等于运行了具有外部参数的accumulator(1,100)。参数是传给accumulator的。
// 101

具体流程解析

  1. sum获取一个返回的函数accumulator
var sum = tco(function(x, y) {
  if (y > 0) {
    return sum(x + 1, y - 1)
  }
  else {
    console.log('shit')
    return x
  }
});

这一步tco(function)返回一个函数accumulator(),具有外部变量value,active,accumulated。 因为accumulator中有使用,所以这些外部变量没有销毁。

  1. sum(1,100) 即调用accumulator(1,100)

  2. 向数组accumulated中push入arguments对象

accumulated.push(arguments);
  1. 此时active为false,进入if语句

  2. active转为true, 然后进入while循环。 这一步很重要,之后每一次调用accumulator函数只会向accumulated数组中传参。而不会进入if语句, 而只有第一次while循环反复调用sum(x+1,y-1)到结束。

即进入这样一个循环 :


while (accumulated.length) {

value = f.apply(this, accumulated.shift())   

sum(x+1,y-1) //即 accumulator(x+1,y-1)

accumulated.push(arguments)

}

一直到y=0时,sum(101,0)返回 101 ,不再向数组accumulated中传入数组,即离开循环。return 101 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值