Git Product home page Git Product logo

copb's Introduction

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.

npm install --save copb

The functions

b

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

import { b } from "https://deno.land/x/copb/mod.ts";

const f = (x: number) => x / 4;
const g = (x: number) => x - 5;

// h = f ∘ g
const h = b(f)(g);

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.

import { b } from "https://deno.land/x/copb/mod.ts";

const f = (x: string) => "number " + x;
const g = (x: number) => String(x);
const h = (x: number) => x / 3;
const l = (x: number) => x - 6;

// m = f ∘ g ∘ h ∘ l
const m = c(o(f)(g)(h)(l));

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.

import { b } from "https://deno.land/x/copb/mod.ts";

const f = (x: string) => "number " + x;
const g = (x: number) => String(x);
const h = (x: number) => x / 3;
const l = (x: number) => x - 6;

// m = f ∘ g ∘ h ∘ l
const m = c(p(l)(h)(g)(f));

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.

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.

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.

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.

copb's People

Contributors

jajaperson avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.