Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including akh.state with all npm packages installed. Try it out:

var akhState = require("akh.state")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

akh.state v0.0.2

Akh state monad and monad transformer

State Monad and Monad Transformer for Akh Javascript Monad Library

The StateT monad transformer adds state to a monad. The base type State provides state on its own.

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

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

Api

StateT and State implement the Fantasy Land]fl monad, monoid, functor, and applicative functor interfaces.

Fantasy Land logo

// State monad
require('akh.state').State
require('akh').State

// State monad transformer
require('akh.state').StateT
require('akh').StateT

State.run(m, s), m.run(s)

Perform a stateful computation m with state s and return state, value pair.

const State = require('akh.state').State

const c = State.of(1)
    .chain(x => State.modify(s => s + x))
    .chain(x => State.of('val'))

run(c, 's') === {'value': 'val', 'state': 's1'}

State.eval(m, s), m.eval(s)

Perform a stateful computation m with state s and return the resulting value.

State.eval(c, 's') === 'val'

State.exec(m, s), m.exe(s)

Perform a stateful computation m with state s and return the resulting state.

State.eval(c, 's') == 's'

StateT.run(m, s)

Same as State.run but for transformed types.

StateT.evalT(m, s)

Same as State.eval but for transformed types.

StateT.exec(m, s)

Same as State.exec but for transformed types.

State Interface

All state operations and methods are defined on both the type and its instances.

M.get

Get the current state.

const State = require('akh.state').State

State.get.run('my state') === { value: 'my state', state: 'my state' }

M.put(s)

Set the current state to s

M.modify(f)

Modify the current state with f that maps the current state to a new state.

Contributing

Contributions are welcome.

To get started:

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

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free