This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
match(1) | |
.with(1, v => v * 2) | |
.with(2, v => v * v) | |
.otherwise(() => -1) | |
.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let vector = { x: 1, y: 1 } | |
if(vector.z != undefined) return 'vector3' | |
if(vector.y != undefined) return 'vector2' | |
return 'vector1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
match(vector) | |
.with({ x: 1, y: 1, z: 1 }, () => 'vector3') | |
.with({ x: 2, y:1 }, () => 'vector2') | |
.with({ x: 1 }, () => 'vector1') | |
.otherwise( () => 'no match') | |
.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Pattern<a> = { [ k in keyof a ]?: Pattern<a[k]> } | |
const match_pattern = <a>(value: a, pattern: Pattern<a>) => typeof(value) != 'object' | |
? value === pattern | |
: Object.keys(pattern).every(k => pattern[k] == undefined ? false : match_pattern(value[k], pattern[k])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Option<a> = { kind: 'none' } | { kind: 'some', value: a } | |
let val: Option<string> = { kind: 'some', value: 'hello' } | |
// Conditinal way of geting the value | |
if(val.kind == 'some') return val.value | |
// Pattern matching way of geting the value | |
match(val) | |
.with({ kind: 'some' }, o => o.value) // TypeError! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with: <p extends Pattern<a>>( | |
pattern: p, | |
expr: (a: Extract<a, typeof pattern>) => b | |
) => match(value, otherwise, [...patterns, [pattern, expr]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type LeastUpperBound<a, b> = b extends a ? b : a extends b ? a : never |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Blog { id: number, title: string } | |
// Will be { id: number, title: string } | { errorMessage: string } | |
let httpResult: any = /* API logic. */ | |
let result = httpResult.errorMesage | |
? new Error(httpResult.errorMessage) | |
: ({ id: httpResult.Id, title: httpResult.Title }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
match<any, Blog | Error>(httpResult) | |
.with({Id: Number, Title: String }, r => ({id: r.Id, title: r.Title})) | |
.with({errorMessage: String}, r => new Error(r.errorMessage)) | |
.otherwise(() => new Error('Client parse error')) | |
.run() |
OlderNewer