JS_02_柯里化

一、概念

只传递给函数的一部分参数来调用它,让它返回一个函数去处理剩余的参数。这个过程叫做柯里化。

二、案例理解柯里化

function add(x, y, z) {
  return x + y + z
}

var result = add(10, 20, 30)
console.log(result)
// add的柯里化
function sum1(x) {
  return function(y) {
    return function(z) {
      return x + y + z
    }
  }
}

var result1 = sum1(10)(20)(30)
console.log(result1)

// 简化柯里化的代码
var sum2 = x => y => z => {
  return x + y + z
}

console.log(sum2(10)(20)(30))

var sum3 = x => y => z => x + y + z
console.log(sum3(10)(20)(30))

三、柯里化的用途

  1. 单一职责原则:一个函数处理的问题尽可能单一,每次传入的参数在单一的函数中进行处理,处理完成之后的下一个函数再次使用处理后的结果。(使处理的问题尽可能简单)

    function add(x, y, z) {
      x = x + 2
      y = y * 2
      z = z * z
      return x + y + z
    }
    
    console.log(add(10, 20, 30))
    
    
    function sum(x) {
      x = x + 2
    
      return function(y) {
        y = y * 2
    
        return function(z) {
          z = z * z
    
          return x + y + z
        }
      }
    }
    
    console.log(sum(10)(20)(30))
    
  2. 逻辑的复用

    案例一:

    function add(x, y, z) {
      x = x + 2
      y = y * 2
      z = z * z
      return x + y + z
    }
    
    console.log(add(10, 20, 30))
    
    
    function sum(x) {
      x = x + 2
    
      return function(y) {
        y = y * 2
    
        return function(z) {
          z = z * z
    
          return x + y + z
        }
      }
    }
    
    console.log(sum(10)(20)(30))
    
    

    案例二:

    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("添加新功能~")
    
    

四、柯里化函数的实现

// 柯里化函数的实现hyCurrying
function hyCurrying(fn) {
  function curried(...args) {
    // 判断当前已经接收的参数的个数, 可以参数本身需要接受的参数是否已经一致了
    // 1.当已经传入的参数 大于等于 需要的参数时, 就执行函数
    if (args.length >= fn.length) {
      // fn(...args)
      // fn.call(this, ...args)
      return fn.apply(this, args)
    } else {
      // 没有达到个数时, 需要返回一个新的函数, 继续来接收的参数
      function curried2(...args2) {
        // 接收到参数后, 需要递归调用curried来检查函数的个数是否达到
        return curried.apply(this, [...args,...args2]) //args.concat(args2)
      }
      return curried2
    }
  }
  return curried
}


function add1(x, y, z) {
  return x + y + z
}
var curryAdd = hyCurrying(add1)


console.log(curryAdd(10, 20, 30))
console.log(curryAdd(10, 20)(30))
console.log(curryAdd(10)(20)(30))
posted @ 2022-02-28 23:32  LL幻  阅读(20)  评论(0)    收藏  举报