为什么这个 reduce working (JS) 的递归定义不是?

Why isn't this recursive definition of reduce working (JS)?

我正在尝试使用递归函数重新定义 Javascript 的归约。 这是我的尝试,这是行不通的。如果任何人都可以稍微改变它以使其工作,那就太好了,因为我会更好地理解它。 (这是 functional-javascript-workshop 中的练习)。

function reduce(arr, fn, initial) {
  if (arr.length) {
    var newArr = arr.slice(1, arr.length);
    return reduce(newArr, fn, fn(arr[0]));
  } else {
    return initial;
  }
}

module.exports = reduce

它给了我以下神秘的错误消息,我不知道如何解释:

/usr/local/lib/node_modules/functional-javascript-workshop/exercises/basic_recursion/exercise.js:13
    prev[curr] = ++prev[curr] || 1
                            ^

TypeError: Cannot create property 'undefined' on string 'exercitation'
    at /usr/local/lib/node_modules/functional-javascript-workshop/exercises/basic_recursion/exercise.js:13:29
    at reduce (/home/david/node-school/functional-workshop/solution.js:7:28)
    at /usr/local/lib/node_modules/functional-javascript-workshop/exercises/basic_recursion/exercise.js:12:10
    at obtainResult (/usr/local/lib/node_modules/functional-javascript-workshop/exercises/runner.js:100:21)
    at Exercise.<anonymous> (/usr/local/lib/node_modules/functional-javascript-workshop/exercises/runner.js:66:27)
    at next (/usr/local/lib/node_modules/functional-javascript-workshop/node_modules/workshopper-exercise/exercise.js:188:19)
    at /usr/local/lib/node_modules/functional-javascript-workshop/node_modules/workshopper-exercise/exercise.js:195:7
    at Exercise.<anonymous> (/usr/local/lib/node_modules/functional-javascript-workshop/exercises/runner.js:34:5)
    at next (/usr/local/lib/node_modules/functional-javascript-workshop/node_modules/workshopper-exercise/exercise.js:188:19)
    at /usr/local/lib/node_modules/functional-javascript-workshop/node_modules/workshopper-exercise/exercise.js:195:7

您需要更新累加器:

function reduce(arr, fn, acc) {
  if (arr.length) {
    var newArr = arr.slice(1, arr.length);
    acc = fn(arr[0], acc) 
    return reduce(newArr, fn, acc);
  } else {
    return acc;
  }
}

console.log(reduce([1,2,3], (val, sum) => sum + val, 0))

您忘记将累加器的当前值即 initial 传递到 fn 调用中。

正如其他人所指出的,您没有将累加器传递给 fn。如果你很好奇,reduce 可以用单个三元 (?:) 表达式表示——此外,arr.slice(1) 将从 1 切到数组的末尾;在这种情况下不需要指定切片的结尾

const reduce = (arr, fn, acc) =>
  arr.length === 0
    ? acc
    : reduce(arr.slice(1), fn, fn(acc, arr[0]))

const add = (x,y) => x + y

console.log(reduce([1,2,3], add, 0)) // 6

因为 arr.length === 0arr.slice(1)arr[1] 在处理数组的函数代码中很常见,所以通常会看到将它们抽象成函数以降低复杂性和认知负担

// isEmpty :: [a] -> Boolean
const isEmpty = arr => arr.length === 0

// head :: [a] -> a
const head = arr => arr[0]

// tail :: [a] -> [a]
const tail = arr => arr.slice(1)

// reduce :: ([a], ((b, a) -> b), b) -> b
const reduce = (arr, fn, acc) =>
  isEmpty(arr)
    ? acc
    : reduce(tail(arr), fn, fn(acc, head(arr)))

// add :: (Number, Number) -> Number
const add = (x,y) => x + y

console.log(reduce([1,2,3], add, 0)) // 6