Skip to content

Instantly share code, notes, and snippets.

@jwoudenberg
Created May 5, 2016 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwoudenberg/80e02ea1e6e3a8db24fd14348c0f2321 to your computer and use it in GitHub Desktop.
Save jwoudenberg/80e02ea1e6e3a8db24fd14348c0f2321 to your computer and use it in GitHub Desktop.
fantasy-do
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