Skip to main content
Module

x/fun/semigroup.ts>struct

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
function struct
import { struct } from "https://deno.land/x/fun@v2.0.0-alpha.10/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

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 }

Parameters

semigroups: [K in keyof O]: Semigroup<O[K]>