-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional.js
More file actions
65 lines (65 loc) · 2.16 KB
/
functional.js
File metadata and controls
65 lines (65 loc) · 2.16 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
export const { Some, None } = (() => {
var _a;
const KEY = Symbol("key");
class Some {
constructor(value) {
this[_a] = null;
this.map = (fn) => {
const value = fn(this[KEY]);
return value instanceof None ? value : _Some(value);
};
this.flatMap = (fn) => {
let flatValue = this[KEY];
if (flatValue instanceof None)
return flatValue;
else if (flatValue instanceof Some)
return flatValue.flatMap(fn);
return fn(flatValue);
};
this.unwrap = (_default) => this[KEY];
this.match = (fnx, fny) => {
const err = () => { throw new Error('Inexhaustive pattern'); };
return (fnx.length && fnx || fny.length && fny || err)(this[KEY]);
};
this.try = (fn) => {
try {
return fn(this[KEY]);
}
catch (_b) {
return None;
}
};
this[KEY] = value;
}
}
_a = KEY;
class None {
constructor() {
this.map = (_) => this;
this.flatMap = (fn) => this;
this.unwrap = (_default) => {
if (_default)
return _default;
throw Error("Cannot unwrap None");
};
this.match = (fnx, fny) => {
const err = () => { throw new Error('Inexhaustive pattern'); };
return (!fnx.length && fnx || !fny.length && fny || err)();
};
this.try = (fn) => {
try {
return fn();
}
catch (_b) {
return None;
}
};
}
}
const _Some = (a) => Object.freeze(new Some(a));
const _None = Object.freeze(new None());
const _Option = (value) => {
return value instanceof None ? value : _Some(value);
};
return { Some: _Some, None: _None };
})()