akh.writer

Akh writer monad and monad transformer


Keywords
monad transformer, monad, akh, writer
License
MIT
Install
npm install akh.writer@0.0.0

Documentation

Writer Monad and Monad Transformer for Akh Javascript Monad Library

The WriterT transformer, WriterT, adds error control to a monad. The base type, Writer, provides error logic on its own.

# To use as standalone package
$ npm install --save akh.writer

# To use as part of akh library
$ npm install --save akh

Usage

WriterT and Writer implement the Fantasy Land monad, functor, and applicative functor interfaces.

Fantasy Land logo

// Writer monad
require('akh.writer').Writer
require('akh').Writer

// Writer monad transformer
require('akh.writer').WriterT
require('akh').WriterT

Writer.run(m, w), m.run(w)

Perform a writer computation m without output monoid w and return { value, output}

WriterT.run(t, w), t.run(w)

Same as Writer.run but for a monad transformer. Returns an Writer value inside of the inner monad.

Writer.exec(m, w), m.exec(w)

Perform a writer computation m without output monoid w and return resulting value

Writer Interface

Writer.tell(b)

WriterT(M).tell(b)

Append b to the output.

Writer.of(4)
    .chain(x => writer.tell(List.of('output1')).map(_ => x)))
    .run(List.zero) === { value: 4, output: List of ['output1'] }

Writer.listen()

WriterT(M).listen()

Produces the current value output pair as { value, output }

m.pass()

t.pass()

Exec current monad which returns a [value, f], then modify current output with f and return value.

m.censor(f)

t.censor(f)

Exec current monad, then modify current output with f and return original value.

Contributing

Contributions are welcome.

To get started:

$ cd akh-writer
$ npm install # install dev packages
$ npm test # run tests