https://github.com/risto-stevcev/do-notation
:arrow_left: Do notation for Fantasy Land monad types
https://github.com/risto-stevcev/do-notation
do do-notation fantasy-land monad notation sugar
Last synced: 19 days ago
JSON representation
:arrow_left: Do notation for Fantasy Land monad types
- Host: GitHub
- URL: https://github.com/risto-stevcev/do-notation
- Owner: Risto-Stevcev
- License: mit
- Created: 2016-03-02T02:15:28.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-04-18T13:36:47.000Z (about 9 years ago)
- Last Synced: 2024-10-31T03:52:03.072Z (7 months ago)
- Topics: do, do-notation, fantasy-land, monad, notation, sugar
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/do-notation
- Size: 6.84 KB
- Stars: 12
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Do Notation
[](https://travis-ci.org/Risto-Stevcev/do-notation)
[](https://coveralls.io/github/Risto-Stevcev/do-notation?branch=master)Do notation for Fantasy Land monad types.
[](https://github.com/fantasyland/fantasy-land/)
## Examples
It uses `yield` to "unbox" the Monad (the `<-` in Haskell), which can then be transformed and fed to the next Monad in the `Do` block. The `Do` function returns the last Monad in the `Do` block:
```js
const Do = require('do-notation')let maybeString = Do(function*() {
let foo = yield S.Maybe.of('foo')
console.log(foo)
// 'foo'let bar = yield S.Maybe.of('bar' + foo)
console.log(bar)
// 'barfoo'let baz = yield S.Maybe.of('baz' + bar)
console.log(baz)
// 'bazbarfoo'}).toString()
console.log(maybeString)
// 'Just("bazbarfoo")'
```## Implementation
The entire implementation is very succinct and simple:
```js
Do = function(generatorFunction) {
const generator = generatorFunction()return function next(error, v) {
const res = generator.next(v)if (res.done)
return res.value
else
return res.value.chain((v) => next(null, v) || res.value.of(v))
}()
}module.exports = { Do: Do }
```