import { createMonad } from "https://deno.land/x/fun@v.2.0.0-alpha.11/monad.ts";
Derive a Monad instance from of, chain, and a Kind. This is the simplest way to get a Monad instance when one has a creation function (of) and a chain function (aka flatMap or bind).
Examples
Example 1
Example 1
import type { Kind, Out } from "./kind.ts";
import { createMonad } from "./monad.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 of = <A>(a: A): Promise<A> => Promise.resolve(a);
const chain = <A, I>(faui: (a: A) => Promise<I>) =>
(ua: Promise<A>): Promise<I> => ua.then(faui);
// Derive a Monad for Promise
const M = createMonad<KindPromise>({ of, chain });
const result = await pipe(
M.of((n: number) => (m: number) => n + m),
M.ap(M.of(1)),
M.ap(M.of(1)),
); // 2
Type Parameters
U extends Kind