项目作者: jajaperson

项目描述 :
A functional composition framework built for TypesScript that provides type safety without the need for the usual countless overloads, thanks to some functional type trickery.
高级语言: TypeScript
项目地址: git://github.com/jajaperson/copb.git
创建时间: 2021-01-29T14:07:23Z
项目社区:https://github.com/jajaperson/copb

开源协议:MIT License

下载


copb Version Build Status

A functional composition helper for TypesScript that provides type safety
without the need for the usual countless overloads, thanks to some
functional type trickery.

This module contains just 4 curried functions: c, o, p, b.

npm install

copb is available on npm as well as https://deno.land/x/copb.

  1. npm install --save copb

The functions

b

b is a simple curried binary composition function, which the other
functions use under the hood.

  1. import { b } from "https://deno.land/x/copb/mod.ts";
  2. const f = (x: number) => x / 4;
  3. const g = (x: number) => x - 5;
  4. // h = f ∘ g
  5. const h = b(f)(g);
  6. console.log(h(13)); // -> 2

c

c for callable is used to build compositional stacks generated by o and
p. Once built, the resultant composed function can be applied in the normal
way. See usage in the sections below.

o

o (named after the mathematical composition symbol) is for more complex
compositions, with the ability to compose any number of uniary functions
together.

  1. import { b } from "https://deno.land/x/copb/mod.ts";
  2. const f = (x: string) => "number " + x;
  3. const g = (x: number) => String(x);
  4. const h = (x: number) => x / 3;
  5. const l = (x: number) => x - 6;
  6. // m = f ∘ g ∘ h ∘ l
  7. const m = c(o(f)(g)(h)(l));
  8. console.log(m(15)); // -> "number 3"

p

p for pipeline is used to pipe the results of functions to each other. It is
identical to o except it is read from left to right.

  1. import { b } from "https://deno.land/x/copb/mod.ts";
  2. const f = (x: string) => "number " + x;
  3. const g = (x: number) => String(x);
  4. const h = (x: number) => x / 3;
  5. const l = (x: number) => x - 6;
  6. // m = f ∘ g ∘ h ∘ l
  7. const m = c(p(l)(h)(g)(f));
  8. console.log(m(15)); // -> "number 3"

Usage notes

Applying a layer of abstraction, you can think of the o and p functions as
having nodes within the compositional stack. Each of these nodes is denoted in
a seperate set of brackets.

  1. c(o(node1)(node2)(node3));

When necessary, type annotations can be placed between nodes. For o, each
annotation somewhat counterintuitively represents the input type of the
following node, and the second annotation of the first node represents the final
result.

  1. const m = c(o<string, string>(f)<number>(g)<number>(h)<number>(l));

For p, each annotation represents the output type of the following node, and
the second annotation of the first node represents the final input.

  1. const m = c(p<number, number>(l)<number>(h)<string>(g)<string>(f));

API

Generated API documentation, with pseudohaskell illustrations of type
signatures, is available
here.

Comments

Intellisense

Your IDE will probably show a disgusting, monstrosity of a type signature.
That’s because this project uses recursive types in order to provide robust type
safety. Type checking is still completely functional.