1.0.1 • Published 6 years ago
fmap-null-undefined v1.0.1
fmap-null-undefined
Strongly typed, curried functions inspired by Haskell's fmap
(for Maybe
).
Installation
npm install --save fmap-null-undefined
Usage
Given a function of type A => B
, fmap
creates a function of type A | null | undefined => B | null | undefined
.
Use fmap_null
or fmap_undefined
if you want a more specific type (e.g. A | null => B | null
).
import { fmap, fmap_null, fmap_undefined } from "fmap-null-undefined";
const show = (x: number): string => x.toString();
fmap(show)(5) === "5";
fmap(show)(null) === null;
fmap(show)(undefined) === undefined;
fmap_null(show)(5) === "5";
fmap_null(show)(null) === null;
fmap_null(show)(undefined); // type error
fmap_undefined(show)(5) === "5";
fmap_undefined(show)(undefined) === undefined;
fmap_undefined(show)(null); // type error
Partial Application
The functions are curried to simplify partial application.
const fmapShow = fmap(show);
fmapShow(5) === "5";
[ 5, null, undefined, 10 ].map(fmapShow); // [ "5", null, undefined, "10" ]
TypeScript
The package is primarily intended for use with TypeScript. Declarations are included.