Created
May 5, 2016 16:46
-
-
Save jwoudenberg/80e02ea1e6e3a8db24fd14348c0f2321 to your computer and use it in GitHub Desktop.
fantasy-do
This file contains 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 Task = require('data.task') | |
// Using the Task Monad as an example, but any Monad would work. | |
const getAnswer = () => Task.of(42) | |
const even = n => Task.of((n % 2) === 0) | |
const result = fy(function * () { | |
// Yield monads to get back their values. | |
const answer = yield getAnswer() | |
const halfAnswer = answer / 2 | |
// The returned value is passed out. | |
return even(halfAnswer) | |
}) | |
result.fork( | |
e => console.log('ERROR:', e), | |
x => console.log(x) | |
) | |
function fy (generatorFunction) { | |
const generator = generatorFunction() | |
const step = (nextValue) => { | |
const { value, done } = generator.next(nextValue) | |
if (done) { | |
return value | |
} else { | |
return value.chain(step) | |
} | |
} | |
return step() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment