js函数式编程curry与compose实现

//自行实现以下curry函数和compose
//curry
function curry(fn) {
return function aa (...arg) {
if (arg.length >= fn.length) {
return fn(...arg);
} else {
return aa.bind(null, ...arg);
}
}
}
const curry = (fn) => aa = (...arg) => arg.length >= fn.length ? fn(...arg): aa.bind(null, ...arg);
// compose
function compose(...arr) {
return function (...arr2) {
(function aa(n) {
if (n < arr.length -1) {
return arr[n](aa(++n))
} else {
return arr[n](...arr2);
}
})(0)
}
}
const compose = (...arr) => (...arr2) => ((aa = (n) => n < arr.length - 1 ? arr[n](aa(++n)): arr[n](...arr2))(0));

相关文章