Skip to content

Instantly share code, notes, and snippets.

@haohcraft
Last active September 20, 2016 14:10
Show Gist options
  • Save haohcraft/4c83a2aafc3aad3754fb9583681ea365 to your computer and use it in GitHub Desktop.
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/
/*
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