Skip to main content Link Search Menu Expand Document (external link)

Monoid overview

Added in v1.0.0


Table of contents


combinators

array

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

The empty value is the empty array.

Signature

export declare const array: <A>() => Monoid<readonly A[]>

Added in v1.0.0

reverse

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

Signature

export declare const reverse: <A>(M: Monoid<A>) => Monoid<A>

Added in v1.0.0

struct

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

export declare const struct: <R extends { readonly [x: string]: Monoid<any> }>(
  fields: R
) => Monoid<{ readonly [K in keyof R]: [R[K]] extends [Monoid<infer A>] ? A : never }>

Added in v1.0.0

tuple

Similar to Promise.all but operates on Monoids.

[Monoid<A>, Monoid<B>, ...] -> Monoid<[A, B, ...]>

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

export declare const tuple: <T extends readonly Monoid<any>[]>(
  ...elements: T
) => Monoid<{ readonly [I in keyof T]: [T[I]] extends [Monoid<infer A>] ? A : never }>

Added in v1.0.0

constructors

fromSemigroup

Signature

export declare const fromSemigroup: <A>(S: Semigroup<A>, empty: A) => Monoid<A>

Added in v1.0.0

max

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

The empty value is the minimum value.

Signature

export declare const max: <A>(B: Bounded<A>) => Monoid<A>

Added in v1.0.0

min

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

The empty value is the maxBound value.

Signature

export declare const min: <A>(B: Bounded<A>) => Monoid<A>

Added in v1.0.0

type class

Monoid (interface)

Signature

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

Added in v1.0.0