函数式编程

70 阅读1分钟

纯函数

满足以下两个条件就是纯函数:

  1. 确定的输入,返回确定的输出;
  2. 在执行过程中不会产生任何副作用(输入的内容不会被随意篡改)。

柯里化

把一个带多个参数的函数调用,拆分成多个不定个参数的函数调用的过程就叫柯里化。

柯里化的好处:

  • 让函数的职责单一;

image.png

  • 逻辑复用;

image.png

function log(date, type, message) {
  console.log(`[${date.getHours()}:${date.getMinutes()}][${type}]: [${message}]`)
}
// log(new Date(), "DEBUG", "查找到轮播图的bug")
// log(new Date(), "DEBUG", "查询菜单的bug")
// log(new Date(), "DEBUG", "查询数据的bug")

// 柯里化的优化
var log = date => type => message => {
  console.log(`[${date.getHours()}:${date.getMinutes()}][${type}]: [${message}]`)
}
// 如果我现在打印的都是当前时间
var nowLog = log(new Date())
nowLog("DEBUG")("查找到轮播图的bug")
nowLog("FETURE")("新增了添加用户的功能")

var nowAndDebugLog = log(new Date())("DEBUG")
nowAndDebugLog("查找到轮播图的bug")
nowAndDebugLog("查找到轮播图的bug")
nowAndDebugLog("查找到轮播图的bug")
nowAndDebugLog("查找到轮播图的bug")


var nowAndFetureLog = log(new Date())("FETURE")
nowAndFetureLog("添加新功能~")

柯里化函数实现

function toCurring(fn){
  return function curried(...arg1){
    // 判断调用的时候传入的参数大于等于fn需要的参数时,直接调用fn
    if(arg1.length >= fn.length){
      // 改变this指向
      return fn.apply(this, arg1)
    }else{
      // 参数不够递归再调用一次curried方法,且返回一个新函数接收剩余参数
      return function(...arg2){
        // 把前面调用传入的参数拼起来再传进去
        return curried.apply(this, [...arg1, ...arg2])
      }
    }
  }
}
// 使用:
function foo(a, b, c, d){
  console.log(a+b+c+d);
}
foo(3,4,5,6) // 18
// 柯里化
var cFoo = toCurring(foo)
cFoo(3)(4)(5)(6) // 18
cFoo(3)(4,5)(6) // 18
cFoo(3,4,5)(6) // 18