Last active
January 23, 2019 08:58
-
-
Save supasympa/4a52946cf3c68ca123590f50df89c7b1 to your computer and use it in GitHub Desktop.
Functor
This file contains hidden or 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
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