Skip to content

Instantly share code, notes, and snippets.

@carlosrberto
Last active December 14, 2017 01:40
Show Gist options
  • Save carlosrberto/d9371b16faad3fa72baeabb59bf164db to your computer and use it in GitHub Desktop.
Save carlosrberto/d9371b16faad3fa72baeabb59bf164db to your computer and use it in GitHub Desktop.
A simple Curry implementation in JavaScript
const curry = function(fn) {
const partial = function(prevArgs = []) {
return function() {
const nextArgs = [...prevArgs, ...arguments];
if((prevArgs.length + arguments.length) === fn.length) {
return fn.apply(null, nextArgs)
} else {
return partial(nextArgs);
}
}
}
return partial();
};
const sum = (a, b, c) => {
return a + b + c;
}
const sumC = curry(sum);
sumC(1)(2, 3) // 6
const _pipe = function(args) {
return function() {
let result;
args.forEach((fn, i) => {
if(i === 0) {
result = fn.apply(null, arguments);
} else {
result = fn.call(null, result);
}
});
return result;
}
}
const pipe = function() {
return _pipe(Array.from(arguments))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment