Détail du package

akh

mattbierner2.7kMIT3.1.2

Monad and Monad Transformer Collection

monad transformer, algebraic, fantasy land, monad

readme

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.

changelog

ChangeLog

3.1.2 - July 23, 2017

  • Pick up minor fixes for State

3.1.1 - March 13, 2017

  • Pick up minor fixes for Maybe and Writer

3.1.0 - September 3, 2016

  • Added Maybe
  • Added Reader
  • Added Writer

3.0.0 - September 2, 2016

  • Split out into individual libraries.
  • Cleaned up and simplified namespacing.
    • Types now are in akh.Type and `akh.TypeT
    • Core methods are now in top level akh.next
  • Added m.run() instance methods to all types.

2.1.0 - November 20, 2015

  • Added EitherT.runEitherT and Either.runEither to extract either values without needing to pass in callback functions.
  • Updated some internal structures to make it more clear that some values are private.

2.0.1 - November 18, 2015

  • Fixed exec state returning value instead of state.
  • Made sure all code uses modern Khepri.

2.0.0 - April 13, 2014

  • Split interfaces into own files. akh::spec::*.
  • Autolifting of state get put and modify in most base types.
  • Added Codensity monad.
  • Removed trampoline.
  • Split StateT into two files.
    • akh::trans::state has the main, stack safe impl which auto wraps in a Codensity.
    • akh::trans::statei has the actual impl, which is stack unsafe but should be used in stacks so as not to dup the Codensity logic.

1.1.2 - April 10, 2014

  • Performance improvements though recompile with khepri V0.23.0

1.1.1 - April 7, 2014

  • Performance improvements though recompile with khepri V0.21.13.

1.1.0 - April 3, 2014

  • Added base::liftA and base::liftA2 to lift to an applicative.
  • Added derived applicative operation ac that curries f with the argument a.

1.0.1 - April 3, 2014

  • Fixed derived types of map and ap on monad using functions instead of methods.

1.0.0 - April 1, 2014

  • Removed one extra function call per each chain/concat/map/ap.
  • structures now expect the method version of binary functions.

0.8.0 - March 24, 2014

  • Transformers expose their inner type with inner since there is no type system to recover this information.
  • Transforming and Transformer will generated have an additional method liftInner on the output to lift from the inner Transformer's inner to the outer transformer. liftInner can be chained to lift multiple levels.

    var M = StateT (StateT (StateT (StateT Identity))));
    
    // Lift get from inner most state.
     M.liftInner.liftInner(M.inner.inner.inner.get);
     `

0.7.1 - March 24, 2014

  • Fixed Unique possibly blowing up stack for large computations.

0.7.0 - March 24, 2014

  • Allow UniqueT to be passed a seed value.
  • Fixed IdentityT concat.

0.6.0 - March 23, 2014

  • Added IdentityT to transform a monad to itself.
  • Added Trampoline monad.
  • More work on call stack.

0.5.3 - March 22, 2014

  • Fixed call stack on EitherT and ErrorT.
  • Performance improvements.

0.5.2 - March 22, 2014

  • Fixed call stack on StateT.

0.5.1 - March 21, 2014

  • Fixed ContT.lift and DContT.lift.

0.5.0 - March 21, 2014

  • Temp fix for call stack on ContT and DContT.
    • Wrap other structures for long running computations in ContT.
  • Added State.modify.
  • Added IdentityT.
  • Added top level generic versions of ops in base that determine type using argument.
  • Split out DContT's unique id the logic to its own monad and transformer.

0.4.0 - March 20, 2014

  • Added EitherT.
  • Added ErrorT
  • Added Either
  • Added Error.

0.3.0 - March 20, 2014

  • Fixed #5 nu-stream not included.
  • Added applicative functor to all monads using derived interfaces (thanks joneshf).

0.2.0 - March 19, 2014

  • Added AMD files.
  • Fixed Identity using old import.
  • Identity as functor.
  • ContT and Cont as functor.
  • DContT and DCont as functor.
  • ListT and List as functor.
  • StateT and State as functor.

0.1.0 - March 19, 2014

  • Initial release.