Skip to content

Instantly share code, notes, and snippets.

@gonzaloruizdevilla
Forked from 140bytes/LICENSE.txt
Last active August 29, 2015 14:08
Show Gist options
  • Save gonzaloruizdevilla/682e32aa12d25b42d908 to your computer and use it in GitHub Desktop.
Save gonzaloruizdevilla/682e32aa12d25b42d908 to your computer and use it in GitHub Desktop.
JavaScript curry function in 136 bytes

currying in less than 140byt.es

A tweet-sized function for currying other functions with JavaScript

First version was made in exactly 140 bytes. Current version with 4 bytes less (136 bytes in total) is based on @javiervelezreye version (http://repl.it/2fA/4)

about 140byt.es

140byt.es is a tweet-sized, fork-to-play, community-curated collection of JavaScript

140byt.es rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid JavaScript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

var curry = function(
a //function to be curried
){
return function b( //auxiliar function
c //array with stored arguments
){
return
c.length<a.length //if we don't have enough arguments
? function () { // then return a new function
// that will capture new arguments to
// combine them with the stored arguments
// in a new array that will be used in the next
// call of the auxiliar function
return b(
c.concat([].slice.call(arguments))
)
}
: a.apply(this,c) //else call original function
// with stored arguments
}([]) //call auxiliar function with empty array
//to start argument storage
}
function(a){return function b(c){return c.length<a.length?function(){return b(c.concat([].slice.call(arguments)))}:a.apply(this,c)}([])}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2014 Gonzalo Ruiz de Villa <adesis.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": "curry",
"description": "JavaScript curry function, for functional programming, you know.",
"keywords": [
"curry",
"fp",
"functional"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>3, 4, 5, 6</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var curry = function(a){return function b(c){return c.length<a.length?function(){return b(c.concat([].slice.call(arguments)))}:a.apply(this,c)}([])};
var add = curry(function (a,b){return a + b});
var add2 = add(2);
document.getElementById( "ret" ).innerHTML = [1,2,3,4].map(add2).join(', ');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment