import { struct } from "https://deno.land/x/fun@v2.0.0-alpha.6/semigroup.ts";
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,
Examples
Example 1
Example 1
import type { Semigroup } from "./semigroup.ts";
import * as SG from "./semigroup.ts";
import * as N from "./number.ts";
import { pipe } from "./fn.ts";
type Person = { name: string, age: number };
const person = (name: string, age: number): Person => ({ name, age });
// Chooses the longest string, defaulting to left when equal
const longestString: Semigroup<string> = {
concat: (right) => (left) => right.length > left.length ? right : left,
};
// This semigroup will merge two people, choosing the longest
// name and the oldest age
const { concat } = SG.struct<Person>({
name: longestString,
age: N.SemigroupNumberMax,
})
const brandon = pipe(
person("Brandon Blaylock", 12),
concat(person("Bdon", 17)),
concat(person("Brandon", 30))
); // brandon === { name: "Brandon Blaylock", age: 30 }
Type Parameters
O extends ReadonlyRecord<any>