Skip to content

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

Posted on:January 9, 2019 at 01:12 PM

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);
    }
}