Skip to content

Monoid

8 exports Added in v0.24.0 Source

Combinators

array

Added in v0.24.0 Source

Given a type A, this function creates and returns a Semigroup for ReadonlyArray<A>.

The empty value is the empty array.

Signature

declare function array<A>(): Monoid<readonly Array<A>>

reverse

Added in v0.24.0 Source

The dual of a Monoid, obtained by swapping the arguments of combine.

Signature

declare function reverse<A>(M: Monoid<A>): Monoid<A>;

struct

Added in v0.24.0 Source

This function creates and returns a new Monoid for a struct of values based on the given Monoids for each property in the struct. The returned Monoid combines two structs of the same type by applying the corresponding Monoid passed as arguments to each property in the struct.

The empty value of the returned Monoid is a struct where each property is the empty value of the corresponding Monoid in the input monoids object.

It is useful when you need to combine two structs of the same type and you have a specific way of combining each property of the struct.

Signature

declare function struct<
  R extends {
    [x: string]: Monoid<any>;
  },
>(fields: R): Monoid<{ [K in string | number | symbol]: [R[K]] extends [Monoid<A>] ? A : never }>;

tuple

Added in v0.24.0 Source

Similar to Promise.all but operates on Monoids.

This function creates and returns a new Monoid for a tuple of values based on the given Monoids for each element in the tuple. The returned Monoid combines two tuples of the same type by applying the corresponding Monoid passed as arguments to each element in the tuple.

The empty value of the returned Monoid is the tuple of empty values of the input Monoids.

It is useful when you need to combine two tuples of the same type and you have a specific way of combining each element of the tuple.

Signature

declare function tuple<T extends readonly Array<Monoid<any>>>(...elements: T): Monoid<{ [I in string | number | symbol]: [T[I]] extends [Monoid<A>] ? A : never }>

Constructors

fromSemigroup

Added in v0.24.0 Source

Signature

declare function fromSemigroup<A>(S: Semigroup<A>, empty: A): Monoid<A>;

max

Added in v0.24.0 Source

Get a monoid where combine will return the maximum, based on the provided bounded order.

The empty value is the minimum value.

Signature

declare function max<A>(B: Bounded<A>): Monoid<A>;

min

Added in v0.24.0 Source

Get a monoid where combine will return the minimum, based on the provided bounded order.

The empty value is the maxBound value.

Signature

declare function min<A>(B: Bounded<A>): Monoid<A>;

Type Class

Monoid interface

Added in v0.24.0 Source

Signature

interface Monoid<A> extends Semigroup<A> {
  readonly combineAll: (collection: Iterable<A>) => A;
  readonly empty: A;
}