-
Notifications
You must be signed in to change notification settings - Fork 0
/
resultSpec.ts
32 lines (26 loc) · 909 Bytes
/
resultSpec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { ok, flatten, flatMap, Result, isOk } from './result';
import * as assert from 'assert';
function testEqual<T>(a: Result<T>, b: Result<T>): void {
if (isOk(a) && isOk(b)) {
assert(a.val === b.val);
console.log('test passed');
return;
}
assert(false);
}
const byThree = (val: number): Result<number> => ok(val * 3);
const byFour = (val: number): Result<number> => ok(val * 4);
// monadic laws
const a = 3;
const f = byThree;
// Left identity: unit(a).bind(f) === f(a)
// flatMap(f, some(a)) === f(a);
testEqual(flatMap(f, ok(a)), f(a));
const m = ok(3);
// Right identity: m.bind(unit) === m;
// flatMap(resolve, m) === m;
testEqual(flatMap(ok, m), m);
const g = byFour;
// Associativity: m.bind(f).bind(g) === m.bind(x => f(x).bind(g))
// flatMap(g, flatMap(f, m)) === flatMap(x => flatMap(g, f(x)), m);
testEqual(flatMap(g, flatMap(f, m)), flatMap(x => flatMap(g, f(x)), m));