Magma overview

A Magma is a pair (A, concat) in which A is a non-empty set and concat is a binary operation on A

See Semigroup for some instances.

combinators

endo

Signature

export declare const endo: <A>(f: Endomorphism<A>) => (M: Magma<A>) => Magma<A>

filterFirst

Signature

export declare const filterFirst: <A>(predicate: Predicate<A>) => (M: Magma<A>) => Magma<A>

filterSecond

Signature

export declare const filterSecond: <A>(predicate: Predicate<A>) => (M: Magma<A>) => Magma<A>

reverse

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

Signature

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

Example

import { reverse, concatAll } from 'fp-ts/Magma'
import * as N from 'fp-ts/number'

const subAll = concatAll(reverse(N.MagmaSub))(0)

assert.deepStrictEqual(subAll([1, 2, 3]), 2)

type classes

Magma (interface)

Signature

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

utils

concatAll

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

If as is empty, return the provided startWith value.

Signature

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

Example

import { concatAll } from 'fp-ts/Magma'
import * as N from 'fp-ts/number'

const subAll = concatAll(N.MagmaSub)(0)

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