|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- import { getMonoid as getFM } from './function';
- import { getMonoid as getEM } from './Endomorphism';
- import * as _ from './internal';
- import * as Se from './Semigroup';
- // -------------------------------------------------------------------------------------
- // constructors
- // -------------------------------------------------------------------------------------
- /**
- * Get a monoid where `concat` will return the minimum, based on the provided bounded order.
- *
- * The `empty` value is the `top` value.
- *
- * @example
- * import * as N from 'fp-ts/number'
- * import * as M from 'fp-ts/Monoid'
- *
- * const M1 = M.min(N.Bounded)
- *
- * assert.deepStrictEqual(M1.concat(1, 2), 1)
- *
- * @category constructors
- * @since 2.10.0
- */
- export var min = function (B) { return ({
- concat: Se.min(B).concat,
- empty: B.top
- }); };
- /**
- * Get a monoid where `concat` will return the maximum, based on the provided bounded order.
- *
- * The `empty` value is the `bottom` value.
- *
- * @example
- * import * as N from 'fp-ts/number'
- * import * as M from 'fp-ts/Monoid'
- *
- * const M1 = M.max(N.Bounded)
- *
- * assert.deepStrictEqual(M1.concat(1, 2), 2)
- *
- * @category constructors
- * @since 2.10.0
- */
- export var max = function (B) { return ({
- concat: Se.max(B).concat,
- empty: B.bottom
- }); };
- // -------------------------------------------------------------------------------------
- // combinators
- // -------------------------------------------------------------------------------------
- /**
- * The dual of a `Monoid`, obtained by swapping the arguments of `concat`.
- *
- * @example
- * import { reverse } from 'fp-ts/Monoid'
- * import * as S from 'fp-ts/string'
- *
- * assert.deepStrictEqual(reverse(S.Monoid).concat('a', 'b'), 'ba')
- *
- * @since 2.10.0
- */
- export var reverse = function (M) { return ({
- concat: Se.reverse(M).concat,
- empty: M.empty
- }); };
- /**
- * Given a struct of monoids returns a monoid for the struct.
- *
- * @example
- * import { struct } from 'fp-ts/Monoid'
- * import * as N from 'fp-ts/number'
- *
- * interface Point {
- * readonly x: number
- * readonly y: number
- * }
- *
- * const M = struct<Point>({
- * x: N.MonoidSum,
- * y: N.MonoidSum
- * })
- *
- * assert.deepStrictEqual(M.concat({ x: 1, y: 2 }, { x: 3, y: 4 }), { x: 4, y: 6 })
- *
- * @since 2.10.0
- */
- export var struct = function (monoids) {
- var empty = {};
- for (var k in monoids) {
- if (_.has.call(monoids, k)) {
- empty[k] = monoids[k].empty;
- }
- }
- return {
- concat: Se.struct(monoids).concat,
- empty: empty
- };
- };
- /**
- * Given a tuple of monoids returns a monoid for the tuple.
- *
- * @example
- * import { tuple } from 'fp-ts/Monoid'
- * import * as B from 'fp-ts/boolean'
- * import * as N from 'fp-ts/number'
- * import * as S from 'fp-ts/string'
- *
- * const M1 = tuple(S.Monoid, N.MonoidSum)
- * assert.deepStrictEqual(M1.concat(['a', 1], ['b', 2]), ['ab', 3])
- *
- * const M2 = tuple(S.Monoid, N.MonoidSum, B.MonoidAll)
- * assert.deepStrictEqual(M2.concat(['a', 1, true], ['b', 2, false]), ['ab', 3, false])
- *
- * @since 2.10.0
- */
- export var tuple = function () {
- var monoids = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- monoids[_i] = arguments[_i];
- }
- return ({
- concat: Se.tuple.apply(Se, monoids).concat,
- empty: monoids.map(function (m) { return m.empty; })
- });
- };
- // -------------------------------------------------------------------------------------
- // utils
- // -------------------------------------------------------------------------------------
- /**
- * Given a sequence of `as`, concat them and return the total.
- *
- * If `as` is empty, return the monoid `empty` value.
- *
- * @example
- * import { concatAll } from 'fp-ts/Monoid'
- * import * as N from 'fp-ts/number'
- *
- * assert.deepStrictEqual(concatAll(N.MonoidSum)([1, 2, 3]), 6)
- * assert.deepStrictEqual(concatAll(N.MonoidSum)([]), 0)
- *
- * @since 2.10.0
- */
- export var concatAll = function (M) { return Se.concatAll(M)(M.empty); };
- // -------------------------------------------------------------------------------------
- // deprecated
- // -------------------------------------------------------------------------------------
- /**
- * Use [`Monoid`](./void.ts.html#monoid) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var monoidVoid = {
- concat: Se.semigroupVoid.concat,
- empty: undefined
- };
- /**
- * Use [`tuple`](#tuple) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var getTupleMonoid = tuple;
- /**
- * Use [`struct`](#struct) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var getStructMonoid = struct;
- /**
- * Use [`reverse`](#reverse) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var getDualMonoid = reverse;
- /**
- * Use [`max`](#max) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var getJoinMonoid = max;
- /**
- * Use [`min`](#min) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var getMeetMonoid = min;
- /**
- * Use [`concatAll`](#concatall) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var fold = concatAll;
- /**
- * Use [`MonoidAll`](./boolean.ts.html#monoidall) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var monoidAll = {
- concat: Se.semigroupAll.concat,
- empty: true
- };
- /**
- * Use [`MonoidAny`](./boolean.ts.html#monoidany) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var monoidAny = {
- concat: Se.semigroupAny.concat,
- empty: false
- };
- /**
- * Use [`getMonoid`](./function.ts.html#getmonoid) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var getFunctionMonoid = getFM;
- /**
- * Use [`getEndomorphismMonoid`](./function.ts.html#getendomorphismmonoid) instead.
- *
- * **Note**. The execution order in [`getEndomorphismMonoid`](./function.ts.html#getendomorphismmonoid) is reversed.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var getEndomorphismMonoid = function () { return reverse(getEM()); };
- /**
- * Use [`Monoid`](./string.ts.html#monoid) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var monoidString = {
- concat: Se.semigroupString.concat,
- empty: ''
- };
- /**
- * Use [`MonoidSum`](./number.ts.html#monoidsum) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var monoidSum = {
- concat: Se.semigroupSum.concat,
- empty: 0
- };
- /**
- * Use [`MonoidProduct`](./number.ts.html#monoidproduct) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- export var monoidProduct = {
- concat: Se.semigroupProduct.concat,
- empty: 1
- };
|