Last active
December 14, 2017 01:40
-
-
Save carlosrberto/d9371b16faad3fa72baeabb59bf164db to your computer and use it in GitHub Desktop.
A simple Curry implementation in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sum = (a, b, c) => { | |
return a + b + c; | |
} | |
const sumC = curry(sum); | |
sumC(1)(2, 3) // 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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