Skip to content

Instantly share code, notes, and snippets.

@tarasowski
Last active November 6, 2018 09:55
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 tarasowski/7daa6c007784933da0de1f3b45bdb37b to your computer and use it in GitHub Desktop.
Save tarasowski/7daa6c007784933da0de1f3b45bdb37b to your computer and use it in GitHub Desktop.
Make it beautiful with strict rules!
/* 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)
}
}
}
}
@tarasowski
Copy link
Author

Stylin'

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