javascript-堆栈安全的相互递归,而不会在调用方泄漏实现细节

前端之家收集整理的这篇文章主要介绍了javascript-堆栈安全的相互递归,而不会在调用方泄漏实现细节 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我对clojure的循环/递归蹦床进行了广义化,以便可以与间接递归一起使用:

const trampoline = f => (...args) => {
  let acc = f(...args);

  while (acc && acc.type === recur) {
    let [f,...args_] = acc.args;
    acc = f(...args_);
  }

  return acc;
};

const recur = (...args) =>
  ({type: recur,args});

const even = n =>
  n === 0
    ? true
    : recur(odd,n - 1);

const odd = n =>
  n === 0
    ? false
    : recur(even,n - 1);


console.log(
  trampoline(even) (1e5 + 1)); // false

但是,我必须在通话侧明确给蹦床打电话.有没有办法像循环/递归那样再次使其隐式?

顺便说一句,这是循环/重复:

const loop = f => {
  let acc = f();

  while (acc && acc.type === recur)
    acc = f(...acc.args);

  return acc;
};


const recur = (...args) =>
  ({type: recur,args});
最佳答案
显然,由于您希望调用蹦床,因此不能完全跳过它.最简单的事情就是将那些经过抛光处理的调用包装在所需的API中,也许是这样的:

// Utility code
const trampoline = f => (...args) => {
  let acc = f(...args);

  while (acc && acc.type === recur) {
    let [f,args});

// Private implementation
const _even = n =>
  n === 0
    ? true
    : recur(_odd,n - 1);

const _odd = n =>
  n === 0
    ? false
    : recur(_even,n - 1);

// Public API
const even = trampoline(_even);

const odd = trampoline(_odd);

// Demo code
console.log(
  even (1e5 + 1)); // false

console.log(
  odd (1e5 + 1)); // true
原文链接:https://www.f2er.com/js/531262.html

猜你在找的JavaScript相关文章