Skip to content

Instantly share code, notes, and snippets.

@francisrstokes
Created December 29, 2018 00:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francisrstokes/c518d9e97de092a1d7d68cba1aaae451 to your computer and use it in GitHub Desktop.
Save francisrstokes/c518d9e97de092a1d7d68cba1aaae451 to your computer and use it in GitHub Desktop.
Function Composition DSL using generators
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