Skip to content

Instantly share code, notes, and snippets.

@supasympa
Last active January 23, 2019 08:58
Show Gist options
  • Save supasympa/4a52946cf3c68ca123590f50df89c7b1 to your computer and use it in GitHub Desktop.
Save supasympa/4a52946cf3c68ca123590f50df89c7b1 to your computer and use it in GitHub Desktop.
Functor
const log = (i) => {
console.log(i);
return i;
};
const base = v => ({
valueOf: () => v,
toString: () => v.toString()
});
const createFunctor = v => fn => (['map', 'first', 'then', 'finally'].reduce((obj, key) => ({ ...{[key]: fn}, ...obj }), base(v)));
const id = v => createFunctor(v)(fn => id(fn(v)));
const transform10 = id(10);
const add1 = a => a + 1;
const multiplyByTen = b => b * 10;
const divideBy3 = c => (c / 3);
console.log(
transform10
.first(add1)
.then(multiplyByTen)
.finally(divideBy3)
.toString()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment