函数柯里化,就是将一个接受多个参数的函数转化为接受单一参数的函数的技术。
function curry (func) { const args = Array.prototype.slice.call(arguments, 1) return function () { const args2 = Array.prototype.slice.call(arguments)
func.call(this, args.concat(args2))
}
}function add (x, y) { return x + y
}var add3 = curry(add, 3)
add3(2) // 5以上代码中,add3 就是经过柯里化之后的函数。add3 可以理解成是 add 函数的柯里化版本,当调用 add3(2) 时,相当于调用 add(3, 2)。
function curry (func) { // func.length 返回 func 函数定义中形式参数的数量
var arity = func.length return function () { var args = Array.prototype.slice.call(arguments) if (args.length >= arity) { // 实参数量满足形参的情况
func.call(null, args)
} else { // 实参数量少于形参的情况
return function () { var args2 = Array.prototype.slice.call(arguments)
func.call(null, args.concat(args2))
}
}
}
}function add (x, y) { return x + y
}var addCurry = curry(add)
addCurry(3, 2) // 5const add3 = addCurry(3)
add3(2) // 5以上代码中,调用 curry 函数会返回一个函数,例如 addCurry。当我们向 addCurry 函数传入两个参数时,addCurry 与 add 没有区别,但是当我们向 addCurry 函数传入一个参数时,addCurry 与 add 有区别,这时的 addCurry 相当于构造函数,它可以为我们构造出 add 函数的柯里化版本,例如 add3。
const match = curry(function (what, x) { return x.match(what);
})const matchEmail = match(emailRegExp)const matchPhone = match(phoneRegExp)
matchEmail('[email protected]')
matchPhone('15022222222')以上代码中,我们利用 match 函数可以构造出各种不同用途的函数。
const filter = curry(function(f, xs) { return xs.filter(f);
})const filterEmail = filter(item => matchEmail(item))const filterPhone = filter(item => matchPhone(item))const arr = ['[email protected]', '15500000000', '[email protected]', '13022211111', 'hello world']
filterEmail(arr) // ['[email protected]', '[email protected]']filterPhone(arr) // ['15500000000', '13022211111']以上代码中,我们利用 filter 函数可以构造出各种不同用途的过滤函数。以下是更多的例子,可以查看源代码。
add = curry(function (x, y) { return x + y
})
match = curry(function (what, x) { return x.match(what)
})
replace = curry(function (what, replacement, x) { return x.replace(what, replacement)
})
filter = curry(function (f, xs) { return xs.filter(f)
})
map = curry(function map (f, xs) { return xs.map(f)
})
reduce = curry(function (f, a, xs) { return xs.reduce(f, a)
})
split = curry(function (what, x) { return x.split(what)
})
join = curry(function (what, x) { return x.join(what)
})
作者:Karmack
链接:https://www.jianshu.com/p/6836ae9344a9
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦