Skip to main content
Module

x/fun/flatmappable.ts>createFlatmappable

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

Derive a Flatmappable instance from unwrap, flatmap, and a Kind. This is the simplest way to get a Flatmappable instance.

Examples

Example 1

import type { Kind, Out } from "./kind.ts";
import { createFlatmappable } from "./flatmappable.ts";
import { pipe } from "./fn.ts";

// Create a Kind for Promise<A>
interface KindPromise extends Kind {
  readonly kind: Promise<Out<this, 0>>;
};

// Create an of and chain function for Promise<A>
const wrap = <A>(a: A): Promise<A> => Promise.resolve(a);
const flatmap = <A, I>(faui: (a: A) => Promise<I>) =>
  (ua: Promise<A>): Promise<I> => ua.then(faui);

// Derive a Flatmappable for Promise
const M = createFlatmappable<KindPromise>({ wrap, flatmap });

const result = await pipe(
  M.wrap((n: number) => (m: number) => n + m),
  M.apply(M.wrap(1)),
  M.apply(M.wrap(1)),
); // 2

Parameters

unnamed 0: Pick<Flatmappable<U>, "wrap" | "flatmap">