monoid-map

An map as a monoid

Usage no npm install needed!

<script type="module">
  import monoidMap from 'https://cdn.skypack.dev/monoid-map';
</script>

README

Monoid Map

Build Status

Fantasy Land Complaint! Indigenous Complaint!

An implementation of a map as a monoid that expects all of it's elements to also be monoids.

Examples

Simple Example w/ inner lists:

var Map = require("monoid-map");

var first = new Map({
    items: ["potion", "dagger"],
    spells: ["smite", "ignite"]
});

var second = new Map({
    items: ["mallet", "boots"],
    spells: ["exhaust", "teleport"]
});

var merged = first.concat(second);
console.log(merged.toObj());
/*
Prints:
{
    items: ["potion", "dagger", "mallet", "boots"],
    spells: ["smite", "ignite", "exhaust", "teleport"]
}
*/

More complex monoid example:

var Map          = require("monoid-map");
var Max          = require("monoid-max");
var Min          = require("monoid-min");
var Average      = require("monoid-average");
var StdDeviation = require("monoid-std-deviation");

var ages = [22, 23, 21, 25, 27, 30, 25]

var result = ages.map(function(n) {
    return new Map({
        max:     new Max(n),
        min:     new Min(n),
        average: new Average(n),
        stddev:  new StandardDeviation(n)
    });
}).reduce(function(m, n) {
    return m.concat(n);
}).toNative();

console.log(result);
/*
Prints:
{
    max: 30,
    min: 21,
    average: 24.714285714286,
    stddev: 2.86428
}
*/