一时间作息时间调整不过来,终于今天早上7点才起的 !加上工作的事情 现在才开始写今天的总结! 时间20020618 16:00
lodash中的柯里化函数!
_.curry(func) lodash官方文档
功能:创建一个函数,该函数接受一个或多个func的参数,如果func所需要的参数都被提供则执行func并返回执行的结果,否则继续返回改函数并等待接受剩余的参数。
参数:需要柯里化的函数 返回值:柯里化后的函数
// _.curry(func)
const _ = require('lodash')
function getSum (a, b, c) {
return a + b + c
}
const curried = _.curry(getSum)
console.log(curried(1, 2, 3)) // 6
console.log(curried(1)(2, 3))// 6
console.log(curried(1, 2)(3))// 6
案例分析:
// 柯里化案例
// ''.match(/\s+/g)
// ''.match(/\d+/g)
const _ = require('lodash')
const match = _.curry(function (reg, str) {
return str.match(reg)
})
const haveSpace = match(/\s+/g)
console.log(haveSpace('hello world')) // [ ' ' ]
console.log(haveSpace('hello')) // null
const haveNumber = match(/\d+/g)
console.log(haveNumber('123abc456def789')) // [ '123', '456', '789' ]
console.log(haveNumber('jal')) // null
const filter = _.curry(function (func, arr) {
return arr.filter(func)
})
console.log(filter(haveSpace, ['hello world', 'Ji Ailing', 'cathy', 'yibo', 'Wang Yibo']))
// [ 'hello world', 'Ji Ailing', 'Wang Yibo' ]
const findSpace = filter(haveSpace)
console.log(findSpace(['hello world', 'Ji Ailing', 'cathy', 'yibo', 'Wang Yibo']))
// [ 'hello world', 'Ji Ailing', 'Wang Yibo' ]
模拟实现柯里化
function getSum (a, b, c) {
return a + b + c
}
const myCurried = curry(getSum)
console.log(myCurried(1, 2, 3)) // 6
console.log(myCurried(1)(2, 3))// 6
console.log(myCurried(1, 2)(3))// 6
function curry(fn) {
return function curriedFn (...args) {
if(args.length < fn.length) {
return function () {
return curriedFn(...args.concat(Array.from(arguments)))
// return fn(...args, ...arguments) // 这样写也是一样的
}
}else {
return fn(...args)
}
}
}
总结
柯里化可以让我们给一个函数传递较少的参数得到一个已经记住了某些固定参数的新函数 这是一种对函数参数的‘缓存’
让函数变得更灵活,让函数的粒度更细 可以把多元函数转换成一元函数,可以组合使用函数产生强大的功能
函数组合
如果一个函数要经过多个函数处理才能得到最终值,这个时候可以把中间过程的函数合并成一个函数。
- 函数就像是数据的管道,函数组合就是把这些管道连接起来,让数据穿过多个管道形成最终结果
- 函数组合默认是从右到左执行
例子:取到数组中的最后一个值
function compose(f, g) {
return function (value) {
return f(g(value))
}
}
function reverse (arr) {
return arr.reverse()
}
function first (arr) {
return arr[0]
}
const last = compose(first, reverse)
console.log(last([1, 2, 3, 4])) // 4
lodash里的组合函数flow()以及flowRight()
- lodash中组合函数flow()或者flowRight(),他们都可以组合多个函数 flow()是从左到右运行
- flowRight()是从右到左运行,使用的更多一些~
例子:
const _ = require('lodash')
const reverse = arr => arr.reverse()
const first = arr => arr[0]
const toUpper = str => str.toUpperCase()
const f = _.flowRight(toUpper, first, reverse)
console.log(f(['one', 'two', 'three'])) // THREE
学到这里 越来越有感觉 再一步步的演变VUE react 的发展史 个人观点 ! 继续加油!
模拟实现flowRight:
// function compose (...args) {
// return function (value) {
// return args.reverse().reduce(function (acc, fn) {
// return fn(acc)
// }, value)
// }
// }
//reduce() 方法接收一个函数作为累加器~
// 将上面的写法修改成箭头函数
const compose = (...args) => value => args.reverse().reduce((acc, fn) => fn(acc), value)
const reverse = arr => arr.reverse()
const first = arr => arr[0]
const toUpper = str => str.toUpperCase()
const f = compose(toUpper, first, reverse)
console.log(f(['one', 'two', 'three'])) // THREE
函数组合要满足结合律
- 我们既可以把g和h组合,还可以把f和g组合,结果都是一样的
const _ = require('lodash')
const f = _.flowRight(_.toUpper, _.first, _.reverse)
console.log(f(['one', 'two', 'three'])) // THREE
const f2 = _.flowRight(_.flowRight(_.toUpper, _.first), _.reverse)
console.log(f2(['one', 'two', 'three'])) // THREE
const f3 = _.flowRight(_.toUpper, _.flowRight(_.first, _.reverse))
console.log(f3(['one', 'two', 'three'])) // THREE
函数组合的调试
// NEVER SAY DIE --> never-say-die
const _ = require('lodash')
const split = _.curry((sep, str)=>_.split(str, sep))
// 为什么要调换两个参数的位置?因为要保证函数只有一个参数的函数,那就要通过柯里化实现。
// 而柯里化想要保留一个参数,那就只能保留最后一个参数,所以要把str放到最后
const join = _.curry((sep, arr) => _.join(arr, sep))
const map = _.curry((fn, arr) => _.map(arr, fn))
const log = v => {
console.log(v)
return v
}
const trace = _.curry((tag, v) => {
console.log(tag, v)
return v
})
// const f = _.flowRight(join('-'), log, _.toLower, split(' ')) // n-e-v-e-r-,-s-a-y-,-d-i-e
// const f = _.flowRight(join('-'), log, split(' '), _.toLower) // never-say-die
const f = _.flowRight(join('-'), trace('map之后'), map(_.toLower), trace('split之后'), split(' ')) // never-say-die
console.log(f('NEVER SAY DIE'))
告一段落 去吃饭啦 !!!!!时间 20020618 19:33 say good bye !