This RFC proposes a library of point-free functional combinators in TypeScript that leverage native TypeScript types. By reinterpreting existing types (null, undefined, never, arrays, etc.) in a functional context, we can build a lightweight, non-intrusive API that integrates seamlessly with existing TypeScript codebases.
A Maybe type in ts-combinators is a regular TypeScript type, such as: Number, String, Boolean etc that is defined as follows:
type Empty = null | undefined | void | never;
type Maybe<T> = T | Empty;A maybe is any value that is not null, undefined, or void. ts-combinators allows to interact with maybe values with the some or none combinators:
const addOne = pipe(
some((n: number) => n + 1),
none(constant(0))
);
const five = addOne(4); // returns 5
const zero = addOne(null); // returns 0The combinator
constantreturns a delayed value (i.e., function that acceptsvoidand returns some value).
An Either type is any function that returns a two-items array, where the first item is the Left value, usually represents an error, and the second item is the Right value, usually represents a successful result:
type Either<L, R> = () => [L, undefined] | [undefined, R];The following combinators work with an Either type:
rightmaps a right value, if the either is of theRightvariant.leftmaps a left value, if the either is of theLeftvariant.chainRightmaps a right value, returns a newEither.extractreturns the value of theEither(the value that is notundefined).
The IO type represents a delayed computation:
// add :: Number -> Number -> Number
const add = (a: number) => (b: number) => a + b;
// ioAdd :: Number -> Number -> Void -> Number
const ioAdd = io(add);
// Example usage:
const run = ioAdd(2)(3); // returned type: `() => number`;
console.log(run()); // prints `5`ts-combinators does not introduce a new type for arrays; Instead, it implements point-free combinators to work with arrays:
mapArrapplies a functor to every item of the array.filterArrfilters the array based on the provided predicate.reduceArraccepts a curried reducer function, and an initial value. Returns the aggregated result of applying the reducer function to every item of the array.