Skip to main content
Module

x/fun/fn.ts>flatmap

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

Create a new Fn by combining A => L => I with D => A to produce D & L => I. This is equivalent to ap with the first two arguments switched. It is also limited to unary functions in order to properly handle type widening on the input type.

Examples

Example 1

import { pipe, flatmap } from "./fn.ts";
const add = (n: number) => (m: number) => n + m;

const flatmaper = pipe(
  (n: number) => n,
  flatmap(add),
  flatmap(add),
  flatmap(add),
  flatmap(add),
  flatmap(add),
);

const result1 = flatmaper(1); // 6
const result2 = flatmaper(2); // 12
const result3 = flatmaper(3); // 18

Parameters

fati: (a: A) => Fn<L, I>

Returns

<D>(ta: Fn<D, A>) => Fn<D & L, I>