Skip to content

Instantly share code, notes, and snippets.

View WimJongeneel's full-sized avatar
:octocat:

Wim Jongeneel WimJongeneel

:octocat:
View GitHub Profile
@WimJongeneel
WimJongeneel / match.1.ts
Created December 15, 2019 10:46
match-1.ts
match(1)
.with(1, v => v * 2)
.with(2, v => v * v)
.otherwise(() => -1)
.run()
export const match = <a, b>(value: a) => builder<a, b>(value)(() => undefined, [])
const builder = <a, b>(
value: a
) => (
otherwise: () => b = () => null,
patterns: Array<[Pattern<a>, Fun<a, b>]> = []
) => ({
with: (pattern: a, expr: fun<a, b>) =>
builder(value)(otherwise, [...patterns, [pattern, expr]]),