import * as fun from "https://deno.land/x/fun@v.2.0.0-alpha.11/semigroup.ts";
Semigroup is an algebra over associativity and totality. It's basic purpose can be understood as "a way to merge, concatenate, or combine" two of a thing into one thing.
This file contains the Semigroup algebra, a collection of instance getters, and some utilities around Semigroups.
Functions
Given a semigroup, create a function that will iterate through an array of values and concat them. This is not much more than Array.reduce(concat). | |
Create a semigroup that always returns the given value, ignoring anything that it is concatenated with. | |
f dual | Get the "Dual" of an existing Semigroup. This effectively reverses the order of the input semigroup's application. For example, the dual of the "first" semigroup is the "last" semigroup. The dual of (boolean, ||) is itself. |
Get an Semigroup over A that always returns the first parameter supplied to concat (confusingly this is actually the last parameter since concat is in curried form). | |
Create a semigroup that works like Array.join, inserting middle between every two values that are concatenated. This can have some interesting results. | |
f last | Get an Semigroup over A that always returns the last parameter supplied to concat (confusingly this is actually the first parameter since concat is in curried form). |
f max | Create a semigroup fron an instance of Ord that returns that maximum for the type being ordered. This Semigroup functions identically to max from Ord. |
f min | Create a semigroup fron an instance of Ord that returns that minimum for the type being ordered. This Semigroup functions identically to min from Ord. |
Get a Semigroup from a struct of semigroups. The resulting semigroup will operate over similar shaped structs applying the input semigroups applying each based on its position, | |
Get a Semigroup from a tuple of semigroups. The resulting semigroup will operate over tuples applying the input semigroups applying each based on its position, |
Interfaces
A Semigroup is an algebra with a notion of concatenation. This means that it's used to merge two Ts into one T. The only rule that this merging must follow is that if you merge A, B, and C, that it doesn't matter if you start by merging A and B or start by merging B and C. There are many ways to merge values that follow these rules. A simple example is addition for numbers. It doesn't matter if you add (A + B) + C or if you add A + (B + C). The resulting sum will be the same. Thus, (number, +) can be used to make a Semigroup (see SemigroupNumberSum for this exact instance). |
Type Aliases
A type for Semigroup over any, useful as an extension target for functions that take any Semigroup and do not need to extract the type. | |
A type level extractor, used to pull the inner type from a Semigroup. |