Created
December 29, 2018 00:53
-
-
Save francisrstokes/c518d9e97de092a1d7d68cba1aaae451 to your computer and use it in GitHub Desktop.
Function Composition DSL using generators
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 getDSLValue = (iterator, last) => { | |
const {value, done} = iterator.next(last); | |
if (done) { | |
return value.slice(1).reduce((x, f) => f(x), value[0]); | |
} | |
return getDSLValue(iterator, last ? [...last, value] : [value]); | |
} | |
const pipe = gen => getDSLValue(gen(null, )); | |
const square = x => x**2; | |
const add = x => y => x + y; | |
const divideBy2 = x => x / 2; | |
const value = pipe(function*() { | |
yield 5; | |
yield square; | |
yield add(5); | |
const answer = yield divideBy2; | |
return answer; | |
}); | |
console.log(value); | |
// -> 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment