Last active
November 11, 2021 16:05
-
-
Save maniart/25a7a0939d0958c422c7 to your computer and use it in GitHub Desktop.
Infinite Curry
This file contains hidden or 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
/* | |
Implement a function that allows for infinite cyrring, and can be used as such: | |
`_do(fn)(2)(3)(5)(8)(22)(230)(100)(10)();` | |
*/ | |
function _do(fn) { | |
var args = [] | |
, fn; | |
function exec() { | |
return args.reduce(function(prev, curr) { | |
return fn && fn.call(null, prev, curr); | |
}); | |
} | |
function addArg(arg) { | |
if(arg) { | |
args.push(arg); | |
console.log('got another arg! all args: ', args); | |
return addArg; | |
} else { | |
console.log('time to exec!'); | |
return exec(); | |
} | |
} | |
if(arguments.length < 1) { | |
console.log('returning _do'); | |
return _do; | |
} else { | |
console.log('got fn ', fn); | |
fn = arguments[0]; | |
return addArg; | |
} | |
} | |
//Sample use case: | |
function add(num1, num2) { | |
return num1 + num2; | |
} | |
_do(add)(2)(3)(5)(8)(22)(230)(100)(10)(); | |
// or to log: console.log(_do(add)(2)(3)(5)(8)(22)(230)(100)(10)()); |
peteromano
commented
Jun 5, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment