Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Last active March 15, 2019 10:34
Show Gist options
  • Save amatiasq/2e4344792f28611fa499 to your computer and use it in GitHub Desktop.
Save amatiasq/2e4344792f28611fa499 to your computer and use it in GitHub Desktop.
Simple way to recursively curry javascript functions http://jsfiddle.net/amatiasq/osrsomq0/
/**
* @param {Function} fn Function to curry.
* @param {Number} lenght The arguments required to invoke the function. Optional. By default is fn.length
* @returns {Function} The currified function.
*/
function curry(fn, length) {
length = length || fn.length;
return function currified() {
var args = [].slice.call(arguments);
if (args.length === 0)
return currified;
if (args.length >= length)
return fn.apply(this, args);
var child = fn.bind.apply(fn, [this].concat(args));
return curry(child, length - args.length);
};
}
var log = console.log.bind(console);
// empty calls are ignored
curry(log, 1) ()()()()(1); // Logs: 1
// extra args are passed
curry(log, 1) (1, 2, 3, 4); // Logs: 1 2 3 4
// recursive currification
curry(log, 5) (1)(2)(3)(4)(5); // 1 2 3 4 5
// works with all combination
curry(log, 4) (1, 2, 3, 4); // Logs: 1 2 3 4
curry(log, 4) (1, 2, 3)(4); // Logs: 1 2 3 4
curry(log, 4) (1, 2)(3, 4); // Logs: 1 2 3 4
curry(log, 4) (1)(2, 3, 4); // Logs: 1 2 3 4
curry(log, 4) (1, 2)(3)(4); // Logs: 1 2 3 4
curry(log, 4) (1)(2)(3, 4); // Logs: 1 2 3 4
curry(log, 4) (1)(2, 3)(4); // Logs: 1 2 3 4
curry(log, 4) (1)(2)(3)(4); // Logs: 1 2 3 4
// function is not invoked until argument lenght is reached
console.log(curry(log, 4) (1)(2)(3)); // returns function currified() { ... }
// when argument length is reached function is invoked and its result is returned
try {
curry(log, 4) (1)(2)(3)(4)(5); // logs 1 2 3 4 & throws undefined is not a function
} catch(err) {
console.log(err);
}
function sum(a, b) {
return a + b;
}
// argument length detected automatically
console.log(curry(sum)(1)(2));
@itcropper
Copy link

How long do you have to have been coding in JavaScript to think to use ".bind.apply()".

@amatiasq
Copy link
Author

amatiasq commented Nov 6, 2015

@itcropper long, really long time 💀

@adampolar
Copy link

I know this is old but I was just googling for this since it just occurred to me to try recursive function currying myself. I came up with

function addRecursive(a, compute) {
  return compute? a :  (b, compute2) => addRecursive(a + b, compute2);
}
console.log(addRecursive(1)(1)(15)(6)(10)(2, true)); //35

😆
Though i guess this wasn't possible before lambdas in js?

@amatiasq
Copy link
Author

amatiasq commented Apr 11, 2017

@adampolar Yes this was possible before (but a lot more verbose). The implementation you posted has three issues:

  • It requires modification of the function itself, with sum it's easy but try to curry 10 methods with 10 lines each. The code above can curry any function without modifying it and without duplicating code.
  • It's not complete curry since doesn't allow me to send multiple arguments at the same time
  • And it breaks curry definition by forcing to send a new argument to actually execute the operation. The nice part of curry is you just provide the same arguments the function would require if not curried but on different executions.

@adampolar
Copy link

adampolar commented Apr 12, 2017

@amatiasq
All true; to be clear my code was meant for amusement and nothing more!

@devdilson
Copy link

devdilson commented Sep 18, 2017

I was doing some experiments about curry functions and I came up with this implementation:

https://gist.github.com/br1337/15b8e53c8bc4df65c7b7bd6d10749d06

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment