An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

        

# Do Notation

[![Build Status](https://travis-ci.org/Risto-Stevcev/do-notation.svg)](https://travis-ci.org/Risto-Stevcev/do-notation)
[![Coverage Status](https://coveralls.io/repos/github/Risto-Stevcev/do-notation/badge.svg?branch=master)](https://coveralls.io/github/Risto-Stevcev/do-notation?branch=master)

Do notation for Fantasy Land monad types.

[![Fantasy Land](https://github.com/fantasyland/fantasy-land/raw/master/logo.png)](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 }
```