Skip to content

Instantly share code, notes, and snippets.

@larizzatg
Created April 12, 2020 17:09
Show Gist options
  • Save larizzatg/a0df00bd8705c47626f431b7994cf848 to your computer and use it in GitHub Desktop.
Save larizzatg/a0df00bd8705c47626f431b7994cf848 to your computer and use it in GitHub Desktop.
learning how to do a basic compose
const _ = {};
_.compose = (...fns) => {
let result = null;
return (...args) => {
result = args;
for(let i = fns.length - 1; i >= 0; i--) {
let fnResult = fns[i](...result);
result = Array.isArray(fnResult) ? fnResult : [fnResult];
}
return result;
};
};
const consider = name => `I think it could be... ${name}`;
const exclaim = statement => `${statement.toUpperCase()}!`;
const blame = _.compose(consider, exclaim);
const result = blame('you');
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment