Monoid
Simple monoid factory in JavaScript. Compatible with the Fantasy Land JS Spec.
Easily define list reductions by providing an identity value and a binary function. Get a free way to reduce a list of monoids with concatAll
.
Examples
Make a monoid and concat
two monoid values.
var M = ; var Any = M; // Use the monoid instance method; // => Any(true) // Or the free function provided by the Monoid moduleM; // => Any(true) // Reduce an entire list of monoidsM; // => Any(true)M; // => Any(false)
Create a function to reduce a list of values using applyWith
.
var any = M; ; // => true; // => true var Product = M; M; // => 150
Note: M.applyWith(<Monoid>, list)
is equivelent to M.extract(M.concatAll(list.map(<Monoid>)))
.
Use shorthand syntax for defining binaries. Currently available for +
, *
, ||
and &&
.
var Sum = M; var sum = M; // => 53
TODO
- More examples/docs
- Add more built in binaries
- Consider additional shorthand
var Any = M.Monoid(false, '||')
.