Skip to main content
Module

x/fun/monad.ts>createMonad

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 createMonad
import { createMonad } from "https://deno.land/x/fun@v2.0.0-alpha.6/monad.ts";

Derive a Monad instance from of, chain, and a Kind URI. 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

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

// Create a URI for Promise<A>
interface URI 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<URI>({ 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

Parameters

unnamed 0: Pick<Monad<U>, "of" | "chain">