Skip to main content
Module

x/fun/combinable.ts>struct

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
function struct
import { struct } from "https://deno.land/x/fun@v2.0.0/combinable.ts";

Get a Combinable from a struct of combinables. The resulting combinable will operate over similar shaped structs applying the input combinables applying each based on its position,

Examples

Example 1

import type { Combinable } from "./combinable.ts";
import * as SG from "./combinable.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: Combinable<string> = {
  combine: (right) => (left) => right.length > left.length ? right : left,
};

// This combinable will merge two people, choosing the longest
// name and the oldest age
const { combine } = SG.struct<Person>({
  name: longestString,
  age: N.InitializableNumberMax,
})

const brandon = pipe(
  person("Brandon Blaylock", 12),
  combine(person("Bdon", 17)),
  combine(person("Brandon", 30))
); // brandon === { name: "Brandon Blaylock", age: 30 }

Type Parameters

O extends ReadonlyRecord<any>

Parameters

combinables: [K in keyof O]: Combinable<O[K]>