函数式编程里面的基本工具函数实现

compose 实现

function compose(...args) {
  return (result) => {
    return args.reduceRight((result, fn) => {
      return fn(result)
    }, result)
  }
}

pipe 实现

function compose(...args) {
  return (result) => {
    return args.reduce((result, fn) => {
      return fn(result)
    }, result)
  }
}

柯里化实现

function currying(fn, ...args) {
    if (args.length >= fn.length) {
        return fn(...args)
    }
    return function (...args2) {
        return currying(fn, ...args, ...args2)
    }
}

部分应用实现

function partial(fn, ...args) {
    return (..._arg) => {
        return fn(...args, ..._arg);
    }
}
Last modification:January 9th, 2019 at 01:15 pm

Leave a Comment