Skip to content

Instantly share code, notes, and snippets.

@eliperelman
Forked from 140bytes/LICENSE.txt
Created June 21, 2011 20:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eliperelman/1038818 to your computer and use it in GitHub Desktop.
Save eliperelman/1038818 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
}
}
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"
]
}
<!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>
@jed
Copy link

jed commented Jun 22, 2011

you should try your hand at shimming Function.prototype.bind. i tried and couldn't pull it off.

@eliperelman
Copy link
Author

@jed I have tried for a couple hours to no avail. Function.prototype.bind is a toughie. This is what I have so far:

Function.prototype.bind=Date.bind||function(a,b,c,d){b=[].slice.call(arguments,1),c=this,d=function(e){e=b.concat(b.slice.call(arguments));if(!(this instanceof d))return c.apply(a,b);c.apply(this,b)},d.prototype = c.prototype;return d}

@jed
Copy link

jed commented Jun 22, 2011

as a baseline, i think andrea giammarchi's approach is good:

function(a,b,c){b=[c=this].slice.call(arguments,1);return function(){return a.apply(c,b.concat.apply(b,arguments))}}

the hard part is assigning it for bind.

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