Skip to content

Instantly share code, notes, and snippets.

@defims
Forked from eliperelman/LICENSE.txt
Last active June 14, 2016 01:14
Show Gist options
  • Save defims/560241f5fc342d76fa7c39b177aedf13 to your computer and use it in GitHub Desktop.
Save defims/560241f5fc342d76fa7c39b177aedf13 to your computer and use it in GitHub Desktop.
Curry functions in JavaScript for 140byt.es
function (
a, // the function to curry or partially apply
b // placeholder variable for splicing arguments
) {
b = [].slice.call(arguments,1); // convert arguments to an array
return function () { // return a new function which:
return a.apply(this, b.concat(b.slice.call(arguments))) // apply the original function with old arguments combined with new arguments
}
}
export function(a,b){b=[].slice.call(arguments,1);return function(){return a.apply(this,b.concat(b.slice.call(arguments)))}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Eli Perelman <http://eliperelman.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "JavaScriptCurryFunctions140bytes",
"description": "Small function to allow the partial application of JS functions.",
"keywords": [ "curry", "partial", "application", "javascript", "curried" ],
"version": "0.1.0"
}
<!DOCTYPE html>
<title>JavaScript Partial Function Application (Currying)</title>
<script>
var curry = function(a,b){b=[].slice.call(arguments,1);return function(){return a.apply(this,b.concat(b.slice.call(arguments)))}};
// this function takes whatever arguments are passed and adds them together
var adder = function() {
var n = 0, args = [].slice.call(arguments);
for (var i = 0, len = args.length; i < len; i++) {
n += args[i];
}
return n;
};
// logs: 4
console.log(adder(2,2));
// curry adder for later application
var addTwelve = curry(adder, 12);
// logs: 15
console.log(addTwelve(3));
// logs: 25
console.log(addTwelve(3,6,4));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment