Risto-Stevcev / do-notation

:arrow_left: Do notation for Fantasy Land monad types

Home Page:https://www.npmjs.com/package/do-notation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Do Notation

Build Status Coverage Status

Do notation for Fantasy Land monad types.

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:

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:

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 }

About

:arrow_left: Do notation for Fantasy Land monad types

https://www.npmjs.com/package/do-notation

License:MIT License


Languages

Language:JavaScript 100.0%