Skip to content

Semigroup

17 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 returned Semigroup combines two arrays by concatenating them.

Signature

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

struct

Added in v0.24.0 Source

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

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 const struct: <
  R extends {
    [x: string]: Semigroup<any>;
  },
>(
  fields: R,
) => Semigroup<{ [K in keyof R]: [R[K]] extends [Semigroup<infer A>] ? A : never }>;

tuple

Added in v0.24.0 Source

Similar to Promise.all but operates on Semigroups.

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

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 const tuple: <T extends ReadonlyArray<Semigroup<any>>>(
  ...elements: T
) => Semigroup<{ [I in keyof T]: [T[I]] extends [Semigroup<infer A>] ? A : never }>;

Example

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

Constructors

constant

Added in v0.24.0 Source

Signature

declare function constant<A>(a: A): Semigroup<A>;

make

Added in v0.24.0 Source

The combineMany parameter is optional and defaults to a standard implementation. You can provide a custom implementation when performance optimizations are possible.

Signature

declare function make<A>(
  combine: (self: A, that: A) => A,
  combineMany: (self: A, collection: Iterable<A>) => A,
): Semigroup<A>;

max

Added in v0.24.0 Source

Semigroup that returns last maximum of elements.

Signature

declare function max<A>(O: Order<A>): Semigroup<A>;

min

Added in v0.24.0 Source

Semigroup that returns last minimum of elements.

Signature

declare function min<A>(O: Order<A>): Semigroup<A>;

Instances

first

Added in v0.24.0 Source

Always return the first argument.

Signature

declare function first<A = never>(): Semigroup<A>;

Invariant

Added in v0.24.0 Source

Signature

declare const Invariant: invariant.Invariant<SemigroupTypeLambda>;

last

Added in v0.24.0 Source

Always return the last argument.

Signature

declare function last<A = never>(): Semigroup<A>;

Product

Added in v0.24.0 Source

Signature

declare const Product: product_.Product<SemigroupTypeLambda>;

SemiProduct

Added in v0.24.0 Source

Signature

declare const SemiProduct: semiProduct.SemiProduct<SemigroupTypeLambda>;

Other

imap

Added in v0.24.0 Source

Signature

declare const imap: {
  <A, B>(to: (a: A) => B, from: (b: B) => A): (self: Semigroup<A>) => Semigroup<B>;
  <A, B>(self: Semigroup<A>, to: (a: A) => B, from: (b: B) => A): Semigroup<B>;
};

intercalate

Added in v0.24.0 Source

The intercalate API returns a function that takes a Semigroup instance and a separator value, and returns a new Semigroup instance that combines values with the given separator.

This API is useful when you want to combine values with a specific separator. For example, when you want to concatenate an array of strings with a separator string in between.

It is interesting to note that there is no equivalent API in the Monoid module. This is because the value empty, which is required for the Monoid interface, cannot exist.

Signature

declare const intercalate: {
  <A>(separator: A): (S: Semigroup<A>) => Semigroup<A>;
  <A>(S: Semigroup<A>, separator: A): Semigroup<A>;
};

reverse

Added in v0.24.0 Source

The dual of a Semigroup, obtained by flipping the arguments of combine.

Signature

declare function reverse<A>(S: Semigroup<A>): Semigroup<A>;

Type Class

Semigroup interface

Added in v0.24.0 Source

Signature

interface Semigroup<A> {
  readonly combine: (self: A, that: A) => A;
  readonly combineMany: (self: A, collection: Iterable<A>) => A;
}

Type Lambdas

SemigroupTypeLambda interface

Added in v0.24.0 Source

Signature

interface SemigroupTypeLambda extends TypeLambda {
  readonly type: Semigroup<unknown>;
}