@fpjs/overture
Version:
A Javascript prelude
26 lines (25 loc) • 1.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.reduce = exports.isFoldable = exports.foldl = exports.foldMap = exports.filterOut = exports.filter = void 0;
var _base = require("@fpjs/overture/base");
var _applicative = require("@fpjs/overture/algebras/applicative");
var _monoid = require("@fpjs/overture/algebras/monoid");
const isFoldable = x => "fantasy-land/reduce" in x;
exports.isFoldable = isFoldable;
const foldl = f => z => x => {
const op = x["fantasy-land/reduce"];
if (op === undefined) {
throw TypeError(`'${(0, _base.type)(x)}' is not Foldable.`);
}
return op.call(x, (y, x) => f(y)(x), z);
};
exports.foldl = foldl;
const reduce = exports.reduce = foldl;
const foldMap = T => f => foldl(acc => x => (0, _monoid.concat)(acc)(f(x)))((0, _monoid.empty)(T));
exports.foldMap = foldMap;
const filter = T => p => foldMap(T)(x => p(x) ? (0, _applicative.pure)(T)(x) : (0, _monoid.empty)(T));
exports.filter = filter;
const filterOut = T => p => filter(T)((0, _base.compose)(_base.not)(p));
exports.filterOut = filterOut;