Monoid overview

Monoid extends the power of Semigroup by providing an additional empty value.

interface Semigroup<A> {
  readonly concat: (x: A, y: A) => A
}

interface Monoid<A> extends Semigroup<A> {
  readonly empty: A
}

This empty value should be an identity for the concat operation, which means the following equalities hold for any choice of x.

concat(x, empty) = concat(empty, x) = x

Many types that form a Semigroup also form a Monoid, such as numbers (with 0) and strings (with '').

import { Monoid } from 'fp-ts/Monoid'

const monoidString: Monoid<string> = {
  concat: (x, y) => x + y,
  empty: '',
}

Adapted from https://typelevel.org/cats

Added in v2.0.0


Table of contents


constructors

max

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

The empty value is the bottom value.

Signature

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

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)

Added in v2.10.0

min

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

The empty value is the top value.

Signature

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

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)

Added in v2.10.0

model

Monoid (interface)

Signature

export interface Monoid<A> extends Se.Semigroup<A> {
  readonly empty: A
}

Added in v2.0.0

utils

concatAll

Given a sequence of as, concat them and return the total.

If as is empty, return the monoid empty value.

Signature

export declare const concatAll: <A>(M: Monoid<A>) => (as: readonly A[]) => A

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)

Added in v2.10.0

reverse

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

Signature

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

Example

import { reverse } from 'fp-ts/Monoid'
import * as S from 'fp-ts/string'

assert.deepStrictEqual(reverse(S.Monoid).concat('a', 'b'), 'ba')

Added in v2.10.0

struct

Given a struct of monoids returns a monoid for the struct.

Signature

export declare const struct: <A>(monoids: { [K in keyof A]: Monoid<A[K]> }) => Monoid<{ readonly [K in keyof A]: A[K] }>

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 })

Added in v2.10.0

tuple

Given a tuple of monoids returns a monoid for the tuple.

Signature

export declare const tuple: <A extends readonly unknown[]>(
  ...monoids: { [K in keyof A]: Monoid<A[K]> }
) => Monoid<Readonly<A>>

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])

Added in v2.10.0

zone of death

fold

Use concatAll instead.

Signature

export declare const fold: <A>(M: Monoid<A>) => (as: readonly A[]) => A

Added in v2.0.0

getDualMonoid

Use reverse instead.

Signature

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

Added in v2.0.0

getEndomorphismMonoid

Use getEndomorphismMonoid instead.

Note. The execution order in getEndomorphismMonoid is reversed.

Signature

export declare const getEndomorphismMonoid: <A = never>() => Monoid<Endomorphism<A>>

Added in v2.0.0

getFunctionMonoid

Use getMonoid instead.

Signature

export declare const getFunctionMonoid: <M>(M: Monoid<M>) => <A = never>() => Monoid<(a: A) => M>

Added in v2.0.0

getJoinMonoid

Use max instead.

Signature

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

Added in v2.0.0

getMeetMonoid

Use min instead.

Signature

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

Added in v2.0.0

getStructMonoid

Use struct instead.

Signature

export declare const getStructMonoid: <O extends Readonly<Record<string, any>>>(monoids: {
  [K in keyof O]: Monoid<O[K]>
}) => Monoid<O>

Added in v2.0.0

getTupleMonoid

Use tuple instead.

Signature

export declare const getTupleMonoid: <T extends readonly Monoid<any>[]>(
  ...monoids: T
) => Monoid<{ [K in keyof T]: T[K] extends Se.Semigroup<infer A> ? A : never }>

Added in v2.0.0

monoidAll

Use MonoidAll instead.

Signature

export declare const monoidAll: Monoid<boolean>

Added in v2.0.0

monoidAny

Use MonoidAny instead.

Signature

export declare const monoidAny: Monoid<boolean>

Added in v2.0.0

monoidProduct

Use MonoidProduct instead.

Signature

export declare const monoidProduct: Monoid<number>

Added in v2.0.0

monoidString

Use Monoid instead.

Signature

export declare const monoidString: Monoid<string>

Added in v2.0.0

monoidSum

Use MonoidSum instead.

Signature

export declare const monoidSum: Monoid<number>

Added in v2.0.0

monoidVoid

Use Monoid instead.

Signature

export declare const monoidVoid: Monoid<void>

Added in v2.0.0