-
-
Save Alexei-B/9a6abe497d4081917dac to your computer and use it in GitHub Desktop.
curry functions in <140 bytes.
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
a = ( // Name the function for callback later. | |
b, // The function to curry. | |
c, // The scope to call within. (Defaults to global scope.) | |
...d // Arguments. (Defaults to empty array.) | |
) => | |
d.length < b.length // If we don't have enough arguments yet. | |
&& // Use && and || to select to eliminate brackets from arrow function. | |
((...e) => // Return a function. | |
a( // Return the result of callback of curry func. | |
b, // Function. | |
c, // Scope. | |
...d, // Arguments we had already. | |
...e // New arguments. | |
)) | |
|| // Else | |
b.apply(c,d) // Return result of applying arguments and scope to function. |
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
a=(b,c,...d)=>d.length<b.length&&((...e)=>a(b,c,...d,...e))||b.apply(c,d) |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2015 Alexei Barnes <http://alexeibarnes.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. |
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
{ | |
"name": "curry", | |
"description": "Turns functions into chainable curried function objects.", | |
"keywords": [ | |
"curry", | |
"chained", | |
"recursive", | |
"functional", | |
"140bytes" | |
] | |
} |
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
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>10</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var a=(b,c,...d)=>d.length<b.length&&((...e)=>a(b,c,...d,...e))||b.apply(c,d) | |
var obj = function() {}; | |
obj.prototype = { | |
example : function(a, b, c, d) { | |
return a + b + c + d; | |
} | |
}; | |
var Obj = new obj(); | |
document.getElementById("ret").innerHTML = a(Obj.example, Obj, 1)(2)(3, 4); | |
</script> |
Just found out I can save 8 bytes removing the ||window after s because if you pass undefined to apply it will use the global scope.
Save another character with the help of your friendly neighborhood JS code golf caddy:
function c(f,s,a){return(a=a||[]).length<f.length?function(){return c(f,s,a.concat.apply(a,arguments))}:f.apply(s,a)}
@atk nice! I've updated it to match and also renamed the variables to follow the abc's I see a lot on 140bytes.
The abc's is not a neccessarity; it's just easier to remember their order.
Eh, if nothing else consistency is worth effort right?
That's true 👍
Thanks to ECMA6 this is now only 73 bytes, and makes more sense to call.
You can now follow the pattern shown here;
var temp = curry(fn, scope, arg1, arg2)(arg3);
temp(arg4, arg5)(arg6);
Additionally if you don't need a scope argument it can be reduced to 64 bytes:
a=(b,...c)=>c.length<b.length&&((...d)=>a(b,...c,...d))||b(...c)
Golf'd.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to @jed for the examples, I just dropped this to 126 bytes after realising I could name the function as seen here: https://gist.github.com/jed/964769