Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tstodter
tstodter / sumTypeUtilities.ts
Last active October 1, 2019 14:01
Pattern matching for sum types/algebraic data types in Typescript
export type UnionType = {kind: string};
export type UnionMemberByKind<U, K> = Extract<U, { kind: K }>
export type UnionMatchObj<U extends UnionType, Ret> = {
[K in U['kind']]: (unionMember: UnionMemberByKind<U, K>) => Ret
};
export type Merge<M extends {}, N extends {}> = {
[P in Exclude<keyof M, keyof N>]: M[P]
@tstodter
tstodter / algebraicTreeFold.ts
Last active September 16, 2019 01:31
Folding across trees, with algebraic types
type Tree<T> = Leaf<T> | Branch<T> | DatumBranch<T>;
type Leaf<T> = {
kind: 'leaf';
value: T;
};
type Branch<T> = {
kind: 'branch';
children: Array<Tree<T>>;

Keybase proof

I hereby claim:

  • I am tstodter on github.
  • I am tstodter (https://keybase.io/tstodter) on keybase.
  • I have a public key whose fingerprint is 9119 201A 9277 E2C6 B1BE 7EE8 E29D 7396 3903 258E

To claim this, I am signing this object: