在这种情况下,.reduce如何与递归函数一起使用?

时间:2020-05-30 05:48:28

标签: javascript

我试图了解.reduce如何与嵌套/递归函数一起使用。在下面的示例中,console.logs显示.reduce首先编译函数,然后运行它们。我不明白这是怎么发生的,它只是创建一个巨大的嵌套函数,还是做其他事情? 从我所看到和阅读的内容来看,人们说它创建了一个直接替代的组合函数: func3(acc(...args),其中acc = func2(func1(...args));但是结果不是func3(func2(func1(...args))(...args))吗?我能想到的唯一解决方案是,第一个(...args)在替换acc时消失了,因此变成了func3(func2(func1(...args)))

var numOfCallsToPipeFunction = 0;
var headerApplied = false;


// const pipe = (f, g) => (...args) => g(f(...args))
const pipe = function(f,g) {
  
  numOfCallsToPipeFunction++
  console.log('___________________________________________\n')
  console.log(" Call# " + numOfCallsToPipeFunction + " to pipe() function:");
  console.log('___________________________________________\n')
 
  console.log("  pipe() >>> 'f' input Parameter: "+f.name+"()\n")
  console.log("  pipe() >>> 'g' input Parameter: "+ g.name+"()\n")
 
    return function (...args) {
    if (!headerApplied) {
      console.log('=======================================================================\n')
       headerApplied = true
    }
    let retResult = g(f(...args))
 
    console.log("Results after running function(s): \n");
    console.log(f.name,g.name);
    console.log(" \n");
    console.log('Returned Result: ', retResult);
    console.log("\n");
    
    return retResult
  }
}
const startPipe  = function (...fns) {
   console.log('_______________________________________________________\n')
  console.log("StartPipe(): Input Parameters (i.e., ...fns)" )
  console.log('_______________________________________________________\n')
  console.log(fns.map(x=>x.name))
  return fns.reduce(pipe);
} 
startPipe(
  func1,
  func2,
  func3,
  func4,
)(1, 1)
 

function func1(num1, num2) {
   console.log('________________________________________________\n')
   console.log("Executing compose function with Data (...args): ")
   console.log('________________________________________________\n')
  console.log(`func1 called, applying:  ${num1} + ${num2}`)
  return num1 + num2
}
 
function func2(num1) {
  console.log(`func2 called, applying:  ${num1} * 2`)
  return num1*2
}
 
function func3(num1) {
   
   console.log(`func3 called, applying:  ${num1} - 2`)
   return num1-1
}

function func4(num1) {
  
 console.log(`func4 called, applying:  ${num1} + 2`)
 return num1+2
}

1 个答案:

答案 0 :(得分:0)

reduce完成后,您将获得一个组合函数,其中每个函数在执行时都会调用对迭代中最后一个函数的引用。它不执行功能,只是将功能作为对象返回。
从技术上讲这不是递归。没有一个函数在调用自己。

reduce函数基本上类似于以下代码片段:

// an "unwrapped" version of the reduce iteration
f1 = func1
g1 = func2
acc1 = (x,y) =>
  g1(f1(x,y))

f2 = acc1
g2 = func3
acc2 = (x,y) =>
  g2(f2(x,y))

f3 = acc2
g3 = func4
acc3 = (x,y) =>
   g3(f3(x,y))

// acc3 has the resulting function
console.log("typeof acc3:", typeof acc3)
console.log("variable acc3 is a function object:", acc3)
// nothing has been called yet, you just have a function object that has chained calls to the other ones

console.log("calling acc3:")
console.log("result of calling acc3:", acc3(1,1))
console.log("\n\nI can call acc2:")
console.log("result of calling acc2:", acc2(1,1))

// how the spread operator functions:
function spread(...args) {
  console.log(args)
  console.log(...args)
}

spread(1,2,3,4)


function func1(num1, num2) {
   console.log('________________________________________________\n')
   console.log("Executing compose function with Data (...args): ")
   console.log('________________________________________________\n')
  console.log(`func1 called, applying:  ${num1} + ${num2}`)
  return num1 + num2
}
 
function func2(num1) {
  console.log(`func2 called, applying:  ${num1} * 2`)
  return num1*2
}
 
function func3(num1) {
   
   console.log(`func3 called, applying:  ${num1} - 2`)
   return num1-1
}

function func4(num1) {
  
 console.log(`func4 called, applying:  ${num1} + 2`)
 return num1+2
}