Semigroup overview

A Semigroup is a Magma where concat is associative, that is:

Associativiy: concat(concat(x, y), z) <-> concat(x, concat(y, z))

Added in v2.0.0


Table of contents


instances

getDualSemigroup

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

Signature

export declare function getDualSemigroup<A>(S: Semigroup<A>): Semigroup<A>

Example

import { getDualSemigroup, semigroupString } from 'fp-ts/lib/Semigroup'

assert.deepStrictEqual(getDualSemigroup(semigroupString).concat('a', 'b'), 'ba')

Added in v2.0.0

getFirstSemigroup

Signature

export declare function getFirstSemigroup<A = never>(): Semigroup<A>

Added in v2.0.0

getFunctionSemigroup

Signature

export declare function getFunctionSemigroup<S>(S: Semigroup<S>): <A = never>() => Semigroup<(a: A) => S>

Added in v2.0.0

getIntercalateSemigroup

You can glue items between and stay associative

Signature

export declare function getIntercalateSemigroup<A>(a: A): (S: Semigroup<A>) => Semigroup<A>

Example

import { getIntercalateSemigroup, semigroupString } from 'fp-ts/lib/Semigroup'

const S = getIntercalateSemigroup(' ')(semigroupString)

assert.strictEqual(S.concat('a', 'b'), 'a b')
assert.strictEqual(S.concat(S.concat('a', 'b'), 'c'), S.concat('a', S.concat('b', 'c')))

Added in v2.5.0

getJoinSemigroup

Signature

export declare function getJoinSemigroup<A>(O: Ord<A>): Semigroup<A>

Added in v2.0.0

getLastSemigroup

Signature

export declare function getLastSemigroup<A = never>(): Semigroup<A>

Added in v2.0.0

getMeetSemigroup

Signature

export declare function getMeetSemigroup<A>(O: Ord<A>): Semigroup<A>

Added in v2.0.0

getObjectSemigroup

Returns a Semigroup instance for objects preserving their type

Signature

export declare function getObjectSemigroup<A extends object = never>(): Semigroup<A>

Example

import { getObjectSemigroup } from 'fp-ts/lib/Semigroup'

interface Person {
  name: string
  age: number
}

const S = getObjectSemigroup<Person>()
assert.deepStrictEqual(S.concat({ name: 'name', age: 23 }, { name: 'name', age: 24 }), { name: 'name', age: 24 })

Added in v2.0.0

getStructSemigroup

Signature

export declare function getStructSemigroup<O extends ReadonlyRecord<string, any>>(
  semigroups: { [K in keyof O]: Semigroup<O[K]> }
): Semigroup<O>

Added in v2.0.0

getTupleSemigroup

Given a tuple of semigroups returns a semigroup for the tuple

Signature

export declare function getTupleSemigroup<T extends ReadonlyArray<Semigroup<any>>>(
  ...semigroups: T
): Semigroup<{ [K in keyof T]: T[K] extends Semigroup<infer A> ? A : never }>

Example

import { getTupleSemigroup, semigroupString, semigroupSum, semigroupAll } from 'fp-ts/lib/Semigroup'

const S1 = getTupleSemigroup(semigroupString, semigroupSum)
assert.deepStrictEqual(S1.concat(['a', 1], ['b', 2]), ['ab', 3])

const S2 = getTupleSemigroup(semigroupString, semigroupSum, semigroupAll)
assert.deepStrictEqual(S2.concat(['a', 1, true], ['b', 2, false]), ['ab', 3, false])

Added in v2.0.0

semigroupAll

Boolean semigroup under conjunction

Signature

export declare const semigroupAll: Semigroup<boolean>

Added in v2.0.0

semigroupAny

Boolean semigroup under disjunction

Signature

export declare const semigroupAny: Semigroup<boolean>

Added in v2.0.0

semigroupProduct

Number Semigroup under multiplication

Signature

export declare const semigroupProduct: Semigroup<number>

Added in v2.0.0

semigroupString

Signature

export declare const semigroupString: Semigroup<string>

Added in v2.0.0

semigroupSum

Number Semigroup under addition

Signature

export declare const semigroupSum: Semigroup<number>

Added in v2.0.0

semigroupVoid

Signature

export declare const semigroupVoid: Semigroup<void>

Added in v2.0.0

type classes

Semigroup (interface)

Signature

export interface Semigroup<A> extends Magma<A> {}

Added in v2.0.0

utils

fold

Signature

export declare function fold<A>(
  S: Semigroup<A>
): {
  (a: A): (as: ReadonlyArray<A>) => A
  (a: A, as: ReadonlyArray<A>): A
}

Example

import * as S from 'fp-ts/lib/Semigroup'

const sum = S.fold(S.semigroupSum)(0)

assert.deepStrictEqual(sum([1, 2, 3]), 6)

Added in v2.0.0