In JavaScript, it is possible to partially apply functions (or curry a function).
http://stackoverflow.com/questions/113780/javascript-curry-what-are-the-practical-applications
In JavaScript, it is possible to partially apply functions (or curry a function).
http://stackoverflow.com/questions/113780/javascript-curry-what-are-the-practical-applications
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> |