-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
184 lines (152 loc) · 4.98 KB
/
index.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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/** @see https://github.com/fantasyland/fantasy-land#functor */
export interface FunctorNone<T> {
'fantasy-land/map'<U = T>(f: (value: T) => U): None<U>
}
/** @see https://github.com/fantasyland/fantasy-land#functor */
export interface FunctorSome<T> {
'fantasy-land/map'<U = T>(f: (value: T) => U): Some<U>
}
/** @see https://github.com/fantasyland/fantasy-land#chain */
export interface ChainNone<T> {
/* alias for flatMap */
'fantasy-land/chain'<U = T>(f: (value: T) => Option<U>): None<T>
}
/** @see https://github.com/fantasyland/fantasy-land#chain */
export interface ChainSome<T> {
/* alias for flatMap */
'fantasy-land/chain'<U = T>(f: (value: T) => Some<U>): Some<U>
'fantasy-land/chain'<U = T>(f: (value: T) => None<U>): None<T>
}
/** @see https://github.com/fantasyland/fantasy-land#apply */
/** @see https://github.com/fantasyland/fantasy-land#applicative */
export interface ApplicativeNone<T> {
'fantasy-land/ap'(a: Option<(v: T) => T>): None<T>
}
/** @see https://github.com/fantasyland/fantasy-land#apply */
/** @see https://github.com/fantasyland/fantasy-land#applicative */
export interface ApplicativeSome<T> {
'fantasy-land/ap'(a: Option<(v: T) => T>): Some<T>
}
/** @see https://github.com/fantasyland/fantasy-land#monad */
export interface MonadNone<T> extends ApplicativeNone<T>, FunctorNone<T>, ChainNone<T> {}
/** @see https://github.com/fantasyland/fantasy-land#monad */
export interface MonadSome<T> extends ApplicativeSome<T>, FunctorSome<T>, ChainSome<T> {}
export abstract class Option<T> {
abstract flatMap<U>(f: (value: T) => Option<U>): Option<U>
abstract getOrElse<U extends T>(def: U): T | U
abstract isEmpty(): this is None<T>
abstract map<U>(f: (value: T) => U): Option<U>
abstract nonEmpty(): this is Some<T>
abstract orElse<U extends T>(alternative: Option<U>): Option<T> | Option<U>
abstract toString(): string
static of<T = {}>(value?: null | undefined): None<T>
static of<T>(value?: T): Some<T>
static of<T>(value?: T | null | undefined): Option<T> {
return value == null ? new None<T>() : new Some<T>(value)
}
// fantasyland
abstract 'fantasy-land/ap'(a: Option<(v: T) => T>): Option<T>
abstract 'fantasy-land/chain'<U = T>(f: (value: T) => Option<U>): Option<T>
abstract 'fantasy-land/map'<U = T>(f: (value: T) => U): Option<U>
static 'fantasy-land/of'<T>(value: T): Option<T> {
return new Some(value)
}
static 'fantasy-land/empty'<T>() {
return new None<T>()
}
static 'fantasy-land/zero'<T>() {
return new None<T>()
}
}
export class None<T> extends Option<T> implements MonadNone<T> {
flatMap<U>(_f: (value: T) => Option<U>): None<U> {
return new None<U>()
}
getOrElse<U extends T>(def: U) {
return def
}
isEmpty() {
return true
}
map<U>(_f: (value: T) => U): None<U> {
return new None<U>()
}
nonEmpty(): this is Some<T> & false { return false }
orElse<U extends T>(alternative: None<U>): None<T>
orElse<U extends T>(alternative: Some<U>): Some<U>
orElse<U extends T>(alternative: Some<U> | None<U>): None<T> | Some<U> {
if (alternative.nonEmpty()) {
return alternative
}
return this
}
toString() {
return 'None'
}
// fantasyland
'fantasy-land/ap'(_a: Option<(v: T) => T>): None<T> {
return this
}
'fantasy-land/chain'<U = T>(f: (value: T) => Option<U>): None<T>
'fantasy-land/chain'(): None<T> {
return this
}
'fantasy-land/map'<U = T>(_f: (value: T) => U): None<U> {
return new None<U>()
}
static 'fantasy-land/of'<T>() {
return new None<T>()
}
}
export class Some<T> extends Option<T> implements MonadSome<T> {
constructor(private value: T) {
super()
}
flatMap<U = T>(f: (value: T) => Some<U>): Some<U>
flatMap<U = T>(f: (value: T) => None<U>): None<U>
flatMap<U = T>(f: (value: T) => Option<U>): Option<U>
flatMap<U = T>(f: (value: T) => Option<U>): Option<U> {
return f(this.value)
}
get() {
return this.value
}
getOrElse<U extends T>(def: U): T | U {
return (
this.value == null ||
(typeof this.value === 'number' && isNaN(this.value))
) ? def : this.value
}
isEmpty(): this is None<T> & false {
return false
}
map<U>(f: (value: T) => U): Some<U> {
return new Some(f(this.value))
}
nonEmpty(): this is Some<T> & true {
return true
}
orElse<U extends T>(alternative: None<U>): Some<T>
orElse<U extends T>(alternative: Some<U>): Some<U>
orElse<U extends T>(_alternative: Option<U>) {
return this
}
toString() {
return `Some(${this.value})`
}
// fantasyland
'fantasy-land/ap'(a: Some<(t: T) => T>): Some<T> {
return a.flatMap(this.map.bind(this))
}
'fantasy-land/chain'<U = T>(f: (value: T) => Some<U>): Some<U>
'fantasy-land/chain'<U = T>(f: (value: T) => None<U>): None<U>
'fantasy-land/chain'<U = T>(f: (value: T) => Option<U>): Option<U> {
return f(this.value)
}
'fantasy-land/map'<U>(f: (value: T) => U) {
return this.map(f)
}
static 'fantasy-land/of'<T>(value: T) {
return new Some(value)
}
}