函数式编程中两个基本的工具函数 -- curry & compose

const curry = (f, args1 = []) => (...args2) => {
  const args = [ ...args1, ...args2 ]
  return f.length === args.length ? f(...args) : curry(f, args)
}

const compose = (...funcs) => {
  if (funcs.length === 0) {
    return arg => arg
  }
  if (funcs.length === 1) {
    return funcs[0]
  }
  return funcs.reduce((a, b) => (...args) => a(b(...args)))
}

你可能感兴趣的:(函数式编程中两个基本的工具函数 -- curry & compose)