You-Dont-Need-Lodash-Underscore
You-Dont-Need-Lodash-Underscore copied to clipboard
PR welcome for _.curry and _.curryRight?
https://lodash.com/docs/4.17.15#curry
// lodash
var abc = function(a, b, c) {
return [a, b, c];
};
var curried = _.curry(abc);
curried(1)(2)(3);
// => [1, 2, 3]
curried(1, 2)(3);
// => [1, 2, 3]
curried(1, 2, 3);
// => [1, 2, 3]
// Curried with placeholders.
curried(1)(_, 3)(2);
// => [1, 2, 3]
// non-ready for review native example
const curry = (foo, len = foo.length) => (...args) => {
if (args.length >= len) {
return foo(...args);
}
const _foo = (foo, ...nextArgs) => foo(...args, ...nextArgs);
const _len = foo.length - args.length;
return curry(_foo, _len)
}
const add3 = curry((a,b,c) => a + b + c);
add3(1)(2)(4) // 7
add3(1)(2, 4) // 7
add3(1,2,4) // 7
To be discussed:
- placeholders support
-
_.curryRight
Just seeing if there's interest in adding, issue creation was inspired from:
- https://github.com/you-dont-need/You-Dont-Need-Loops
- https://marmelab.com/blog/2018/03/14/functional-programming-1-unit-of-code.html
It sounds a good idea.
Maybe
function curry(callback) {
return function _curry(...args) {
return args.length < callback.length
? _curry.bind(null, ...args)
: callback(...args);
};
}