import { fn } from "https://deno.land/x/fun@v2.0.0-alpha.6/mod.ts";
const { dimap } = fn;
A combination of contramap and map, dimap applies fld to the input of a function and fai to the output.
Examples
Example 1
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"