/akh

Javascript Monad and Monad Transformer Collection

Primary LanguageJavaScriptMIT LicenseMIT

akh

Akh - noun

1) Large flightless bird found in Fantasy Land
2) Javascript monad transformer library

$ npm install --save akh

Overview

Akh is a collection of monad and monad transformers that implement Fantasy Land's interfaces. It is inspired by Haskell's MTL.

Fantasy Land logo

Usage

Akh can either be used as a single library, or you can pick up individual types from split out libraries. See each library for more documentation on that type.

All functions from akh.core are top level exports.

Monad Transformers

  • akh.ContT - Continuation transformer. (Monad, Functor, Applicative Functor)
  • akh.DContT - Delimited continuation transformer. (Monad, Functor, Applicative Functor)
  • akh.EitherT - Either transformer. (Monad, Monoid, Functor, Applicative Functor)
  • akh.ErrorT - Error transformer. (Monad, Monoid, Functor, Applicative Functor)
  • akh.IdentityT - Transforms a monad to itself. (Monad, Functor, Applicative Functor)
  • akh.ListT - List transformer. (Monad, Monoid, Functor, Applicative Functor)
  • akh.MaybeT - Maybe transformer. (Monad, Monoid, Functor, Applicative Functor)
  • akh.ReaderT - Reader transformer. (Monad, Monoid, Functor, Applicative Functor)
  • akh.StateT - State transformer. (Monad, Monoid, Functor, Applicative Functor)
  • akh.UniqueT - Get unique int value (Monad, Monoid, Functor, Applicative Functor)
  • akh.WriterT - Writer transformer. (Monad, Monoid, Functor, Applicative Functor)

Monads

  • akh.Cont - Continuation computation. (Monad, Functor, Applicative Functor)
  • akh.DCont - Delimited continuation computation. (Monad, Functor, Applicative Functor)
  • akh.Either - Either computation. (Monad, Functor, Applicative Functor)
  • akh.Error - Error computation. (Monad, Functor, Applicative Functor)
  • akh.Identity - Identity computation. (Monad, Functor, Applicative Functor)
  • akh.List - List computation. (Monad, Monoid, Functor, Applicative Functor)
  • akh.Maybe - Computation that may produce a value or nothing. (Monad, Monoid, Functor, Applicative Functor)
  • akh.Reader - Reader monad. (Monad, Monoid, Functor, Applicative Functor)
  • akh.State – Stateful computation. (Monad, Functor, Applicative Functor)
  • akh.Unique – Get Unique int (Monad, Monoid, Functor, Applicative Functor)
  • akh.Writer - Writer monad. (Monad, Monoid, Functor, Applicative Functor)

Quick Example

const List = require('akh').List
const StateT = require('akh').StateT

// Define a new monad using the state transformer on the list monad.
const M = StateT(List)

// Define a way to pass values through `M`
const run = (c, state) => List.runList(StateT.runStateT(c, state))


// Create a simple stateful computation with an initial value
const start = M.of('porky')

// Run the stateful computation to get a list of
// value, state pairs
run(start, 'wackyland') === [
    { value: 'porky', state: 'wackyland' }
]

// Let's update the current state using a function
const modifiedState = start.modify(state => state.toUpperCase())

run(modifiedState, 'wackyland') === [
    { value: 'WACKYLAND', state: 'WACKYLAND' }
]

// Note that modify also updated the held value here. We could avoid that
// by instead writing
const modifiedState2 = start
    .chain(currentValue =>
        M.modify(state => state.toUpperCase())
            .map(_ => currentValue))

run(modifiedState2, 'wackyland') === [
    { value: 'porky', state: 'WACKYLAND' }
]

// Now let's start using the list monad and branch the state.
const branched = modifiedState2
    .concat(
        M.put('nuts').map(_ => 100)  // `put` sets the current state
    )
    .concat(
        M.put('squirrel').map(_ => 1)
    )
    .concat(
        M.get // gets the state
    )

run(branched, 'wackyland') === [
    { value: 'porky', state: 'WACKYLAND' },
    { value: 100, state: 'nuts' },
    { value: 1, state: 'squirrel' },
    { value: 'wackyland', state: 'wackyland' }
]


// We can then operate on all states at the same time.
const doubled = branched.map(x => x + x)

run(doubled, 'wackyland') === [
    { value: 'porkyporky', state: 'WACKYLAND' },
    { value: 200, state: 'nuts' },
    { value: 2, state: 'squirrel' },
    { value: 'wackylandwackyland', state: 'wackyland' }
]

Contribute

Improvement and additions to Akh are welcome. Please report any issues or send a pull request.


The Dodo Bird is a Looney Toons character created and owned by Warner Bros.