Home

Awesome

<div align="center"> <img src="https://raw.githubusercontent.com/mattbierner/akh/master/documentation/akh.png" alt="akh" /> </div> <h2 align="center"><b>Akh</b> - <i>noun</i></h2> <p align="center"> 1) Large flightless bird found in <a href="https://github.com/fantasyland/fantasy-land">Fantasy Land</a></br> 2) Javascript <a href="https://en.wikibooks.org/wiki/Haskell/Monad_transformers">monad transformer</a> library </p>
$ 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.

<a href="https://github.com/fantasyland/fantasy-land"> <img src="https://raw.github.com/fantasyland/fantasy-land/master/logo.png" align="right" width="82px" height="82px" alt="Fantasy Land logo" /> </a>

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

Monads

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.


<p align="center"> <i>The Dodo Bird is a Looney Toons character created and owned by Warner Bros.</i> </p>