Akh Javascript Monad Library
List Monad and Monad Transformer forThe list transformer ListT
and the base list type, List
. This uses Javascript arrays which are both finite and not lazy. For working with lazy, potentially infinite lists of values see Stream-m.
# To use as standalone package $ npm install --save akh.either # To use as part of akh library $ npm install --save akh
Usage
ListT
and List
implement the Fantasy Land a monad, monoid, functor, and applicative functor interfaces.
// List monadListList // List monad tranformerListTListT
List.of(x)
Create a list consisting of a single element x
.
const List = List List === 1 List === 1 2 3
List.zero
Empty list.
List === Listzero ===
a.concat(b)
Concatenate list b
onto list a
.
List === 1 2 3 4
a.chain(f)
Flatmap list a
with function f
. f
is mapped over a
, returning a new List
for each element of a
. The resulting list is then flattened.
List === 1 2 2 4
a.map(f)
Regular mapping of list a
with function f
. f
is maps current values to new values.
List === 1 4 9
f.ap(m)
Map the functors in list f
over list m
.
List === 1 4 9
List === 2 4 6 11 12 13 1 4 9
Running
List.run(m)
, m.run()
Run list computation m
, getting the resulting list.
ListT.run(t)
, t.run()
Same as List.run
but for transformed types.
Contributing
Contributions are welcome.
To get started:
$ cd akh-list$ npm install # install dev packages $ npm test # run tests