Last active
September 20, 2016 14:10
-
-
Save haohcraft/4c83a2aafc3aad3754fb9583681ea365 to your computer and use it in GitHub Desktop.
Some Functional Utility Functions from http://randycoulman.com/blog/categories/thinking-in-ramda/
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
/* | |
Partial Application | |
*/ | |
/* | |
Combine functions | |
*/ | |
const either = (...args) => (...dataArgs) => { | |
let result = false; | |
let i = 0; | |
while ( i < args.length) { | |
if (args[i].apply(null, dataArgs)) { | |
result = true; | |
break; | |
} | |
i++; | |
} | |
return result; | |
}; | |
const both = (...args) => (...dataArgs) => { | |
let result = true; | |
let i = 0; | |
while ( i < args.length) { | |
if (!args[i].apply(null, dataArgs)) { | |
result = false; | |
break; | |
} | |
i++; | |
} | |
return result; | |
}; | |
const compose = (...args) => (...objArgs) => { | |
if (!args || !args.length) return objArgs; | |
if (args.length === 1) { | |
return args[0].apply(null, objArgs); | |
} | |
const tail = args.slice(-1)[0]; | |
const rest = args.slice(0, -1); | |
return compose.apply(null, rest)(tail.apply(null, objArgs)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment