Last active
November 6, 2018 09:55
-
-
Save tarasowski/7daa6c007784933da0de1f3b45bdb37b to your computer and use it in GitHub Desktop.
Make it beautiful with strict rules!
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
/* Rules: | |
1. No If Statements (only ternary operators are allowed) | |
2. Function has 0 or 1 Argument | |
3. Function is a Single Return (can be as big as you like) | |
4. No Assignments in Function | |
5. No Side-Effects | |
6. No Shared Variables | |
*/ | |
// version #1 | |
function curry(fn) { | |
return function f1(...args) { | |
return args.length >= fn.length | |
? fn(...args) | |
: (...moreArgs) => f1(...[...args, ...moreArgs]) | |
} | |
} | |
// version #2 | |
function curry(fn) { | |
const arity = fn.length | |
return function f1(...args) { | |
if (args.length >= arity) { | |
return fn(...args) | |
} else { | |
return function f2(...moreArgs) { | |
const newArgs = args.concat(moreArgs) | |
return f1(...newArgs) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stylin'