You-Dont-Need-Lodash-Underscore icon indicating copy to clipboard operation
You-Dont-Need-Lodash-Underscore copied to clipboard

PR welcome for _.curry and _.curryRight?

Open chuckxD opened this issue 5 years ago • 2 comments

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

chuckxD avatar Nov 27 '19 07:11 chuckxD

It sounds a good idea.

cht8687 avatar Nov 28 '19 09:11 cht8687

Maybe

function curry(callback) {
  return function _curry(...args) {
    return args.length < callback.length
      ? _curry.bind(null, ...args)
      : callback(...args);
  };
}

djalmajr avatar Nov 04 '20 15:11 djalmajr