Skip to main content
Module

x/fun/mod.ts>fn.dimap

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 fn.dimap
import { fn } from "https://deno.land/x/fun@v2.0.0-alpha.12/mod.ts";
const { dimap } = fn;

A combination of premap and map, dimap applies fld to the input of a function and fai to the output.

Examples

Example 1

import type { NonEmptyArray } from "./array.ts";
import { dimap, pipe } from "./fn.ts";
import { plural, split } from "./string.ts";

const are = plural("is", "are");
const words = plural("word", "words");
const describe = (n: number) => `There ${are(n)} ${n} ${words(n)}`;

const toWords = split(/\s+/g); // string => string[]
const count = (ws: NonEmptyArray<string>) => ws.length;

const fromString = pipe(
  count,
  dimap(toWords, describe),
);

const result1 = fromString("Hello World"); // "There are 2 words"
const result2 = fromString("Hi"); // "There is 1 word"
const result3 = fromString("This   is    a    test"); // "There are 4 words"

Parameters

fld: (l: L) => D
fai: (a: A) => I

Returns

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