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
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
Type Parameters
U extends Kind
Parameters
unnamed 0: Pick<Flatmappable<U>, "wrap" | "flatmap">