- Large flightless bird found in Fantasy Land
- Javascript monad transformer library.
Akh is a collection of monad and monad transformers that implement Fantasy Land's interfaces. It is inspired by Haskell's MTL.
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.
- 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.StateT - State transformer. (Monad, Monoid, Functor, Applicative Functor)
- akh.UniqueT - Get unique int value (Monad, Monoid, Functor, Applicative Functor)
- 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.State – Stateful computation. (Monad, Functor, Applicative Functor)
- akh.Unique – Get Unique int (Monad, Monoid, Functor, Applicative Functor)
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') === [
{ x: 'porky', s: 'wackyland' }
]
// Let's update the current state using a function
const modifiedState = start.modify(state => state.toUpperCase())
run(modifiedState, 'wackyland') === [
{ x: 'WACKYLAND', s: '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') === [
{ x: 'porky', s: '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') === [
{ x: 'porky', s: 'WACKYLAND' },
{ x: 100, s: 'nuts' },
{ x: 1, s: 'squirrel' },
{ x: 'wackyland', s: 'wackyland' }
]
// We can then operate on all states at the same time.
const doubled = branched.map(x => x + x)
run(doubled, 'wackyland') === [
{ x: 'porkyporky', s: 'WACKYLAND' },
{ x: 200, s: 'nuts' },
{ x: 2, s: 'squirrel' },
{ x: 'wackylandwackyland', s: 'wackyland' }
]
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.