1.3.0 • Published 5 months ago

itsamatch v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

NPM Package Build Size

It's a match!

Itsamatch is a tiny set of types and utilities to define and use variants / tagged unions / sum types in a more declarative way in TypeScript.

Usage

itsamatch exposes three functions (match, matchMany, genConstructors) and a few types that make it easier to construct data types. Below is a simple example showing how it can be used to create a linked list data type :

import { DataType, constructors, match } from 'itsamatch';

// a list is a data type with two variants:
type List<T> = DataType<{
    Nil: {},
    Cons: { head: T, tail: List<T> }
}>;

// generate default variant constructors for lists of numbers
const { Nil, Cons } = constructors<List<number>>().get('Nil', 'Cons');

// use the match function to compute the length of a list
const len = <T>(list: List<T>): number => match(list, {
    Nil: () => 0,
    Cons: ({ tail }) => 1 + len(tail)
});

const same = <T>(a: List<T>, b: List<T>): boolean => matchMany([a, b], {
  'Nil Nil': () => true,
  'Cons Cons': (l, r) => l.head === r.head && same(l.tail, r.tail),
  _: () => false,
});

const size = len(Cons({ head: 1, tail: Cons({ head: 2, tail: Nil() }) })); // 2
const sameElems = same(Cons({ head: 1, tail: Nil() }), Nil()); // false

More examples are available in the /examples folder

1.3.0

5 months ago

1.2.0

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.2

2 years ago

1.0.2

2 years ago

1.0.3

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago