Skip to content

Instantly share code, notes, and snippets.

@lvivski
Forked from 140bytes/LICENSE.txt
Last active December 16, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvivski/5452414 to your computer and use it in GitHub Desktop.
Save lvivski/5452414 to your computer and use it in GitHub Desktop.
сurry.js for 140byt.es
function curry(fn, context, args, a) { // fn — function to curry, context — calling context
args = args || []
return function () { // args are empty if none, return function
return (a = arguments).length // if arguments length
? curry.call(this, fn, context, args.concat.apply(args, a)) // curry function with more arguments
: fn.apply(context, args) // call function in context
}
}
function _(f,c,g,a){g=g||[];return function(){return 0 in(a=arguments)?_.call(_,f,c,g.concat.apply(g,a)):f.apply(c,g)}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Yehor Lvivski <lvivski@gmail.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.js",
"description": "Curry",
"keywords": [
"functional",
"haskell",
"curry"
]
}
<!DOCTYPE html>
<title>Curry.js</title>
<div>Expected value: <b>5</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var myFunction = function _(f,c,g,a){g=g||[];return function(){return 0 in(a=arguments)?_.call(_,f,c,g.concat.apply(g,a)):f.apply(c,g)}}
document.getElementById( "ret" ).innerHTML = myFunction(Math.max, null)( 1, 2, 3 )( 5 )( 4 )()
</script>
@atk
Copy link

atk commented Apr 26, 2013

Save 1 byte by definining g before return (save the brackets), another one by replacing ".length" with "0 in":

function _(f,c,g,a){g=g||[];return function(){return 0 in(a=arguments)?_.call(_,f,c,g.concat.apply(g,a)):f.apply(c,g)}}

@lvivski
Copy link
Author

lvivski commented May 14, 2013

Wow, thanks!

@atk
Copy link

atk commented May 22, 2013

You're welcome!

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