forked from baetheus/fun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datum.ts
297 lines (251 loc) · 6.7 KB
/
datum.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import type { $, Kind, Out } from "./kind.ts";
import type { Alt } from "./alt.ts";
import type { Applicative } from "./applicative.ts";
import type { Monad } from "./monad.ts";
import type { Monoid } from "./monoid.ts";
import type { Ord } from "./ord.ts";
import type { Semigroup } from "./semigroup.ts";
import type { Eq } from "./eq.ts";
import type { Show } from "./show.ts";
import type { Traversable } from "./traversable.ts";
import { fromCompare } from "./ord.ts";
import { isNotNil } from "./nilable.ts";
import { flow, identity, pipe } from "./fn.ts";
/**
* TODO: Lets get a monoid in here for tracking progress.
*/
export type Initial = {
readonly tag: "Initial";
};
export type Pending = {
readonly tag: "Pending";
};
export type Refresh<A> = {
readonly tag: "Refresh";
readonly value: A;
};
export type Replete<A> = {
readonly tag: "Replete";
readonly value: A;
};
export type Datum<A> = Initial | Pending | Refresh<A> | Replete<A>;
export type None = Initial | Pending;
export type Some<A> = Refresh<A> | Replete<A>;
export type Loading<A> = Pending | Refresh<A>;
export interface KindDatum extends Kind {
readonly kind: Datum<Out<this, 0>>;
}
export const initial: Initial = { tag: "Initial" };
export const pending: Pending = { tag: "Pending" };
export function refresh<D>(value: D): Datum<D> {
return ({ tag: "Refresh", value });
}
export function replete<D>(value: D): Datum<D> {
return ({ tag: "Replete", value });
}
export function constInitial<A = never>(): Datum<A> {
return initial;
}
export function constPending<A = never>(): Datum<A> {
return pending;
}
export function fromNullable<A>(a: A): Datum<NonNullable<A>> {
return isNotNil(a) ? replete(a) : initial;
}
export function tryCatch<A>(fa: () => A): Datum<A> {
try {
return replete(fa());
} catch (_) {
return initial;
}
}
export function toLoading<A>(ta: Datum<A>): Datum<A> {
return pipe(
ta,
match(
constPending,
constPending,
refresh,
refresh,
),
);
}
export function isInitial<A>(ta: Datum<A>): ta is Initial {
return ta.tag === "Initial";
}
export function isPending<A>(ta: Datum<A>): ta is Pending {
return ta.tag === "Pending";
}
export function isRefresh<A>(ta: Datum<A>): ta is Refresh<A> {
return ta.tag === "Refresh";
}
export function isReplete<A>(ta: Datum<A>): ta is Replete<A> {
return ta.tag === "Replete";
}
export function isNone<A>(ta: Datum<A>): ta is None {
return isInitial(ta) || isPending(ta);
}
export function isSome<A>(ta: Datum<A>): ta is Some<A> {
return isRefresh(ta) || isReplete(ta);
}
export function isLoading<A>(ta: Datum<A>): ta is Loading<A> {
return isPending(ta) || isRefresh(ta);
}
export function match<A, B>(
onInitial: () => B,
onPending: () => B,
onReplete: (a: A) => B,
onRefresh: (a: A) => B,
) {
return (ma: Datum<A>): B => {
switch (ma.tag) {
case "Initial":
return onInitial();
case "Pending":
return onPending();
case "Refresh":
return onRefresh(ma.value);
case "Replete":
return onReplete(ma.value);
}
};
}
export function getOrElse<A>(onNone: () => A) {
return match<A, A>(onNone, onNone, identity, identity);
}
export function of<A>(a: A): Datum<A> {
return replete(a);
}
export function throwError<A = never>(): Datum<A> {
return initial;
}
export function map<A, I>(fai: (a: A) => I): (ta: Datum<A>) => Datum<I> {
return match(
constInitial,
constPending,
flow(fai, replete),
flow(fai, refresh),
);
}
export function ap<A>(
ua: Datum<A>,
): <I>(ufai: Datum<(a: A) => I>) => Datum<I> {
switch (ua.tag) {
case "Initial":
return (ufai) => isLoading(ufai) ? pending : initial;
case "Pending":
return constPending;
case "Replete":
return (ufai) =>
isReplete(ufai)
? replete(ufai.value(ua.value))
: isRefresh(ufai)
? refresh(ufai.value(ua.value))
: isLoading(ufai)
? pending
: initial;
case "Refresh":
return (ufai) => isSome(ufai) ? refresh(ufai.value(ua.value)) : pending;
}
}
export function chain<A, I>(
fati: (a: A) => Datum<I>,
): (ta: Datum<A>) => Datum<I> {
return match(
constInitial,
constPending,
fati,
flow(fati, toLoading),
);
}
export function join<A>(taa: Datum<Datum<A>>): Datum<A> {
return pipe(taa, chain(identity));
}
export function alt<A>(tb: Datum<A>): (ta: Datum<A>) => Datum<A> {
return (ta) => isSome(ta) ? ta : tb;
}
export function reduce<A, O>(
foao: (o: O, a: A) => O,
o: O,
): (ta: Datum<A>) => O {
return (ta) => isSome(ta) ? foao(o, ta.value) : o;
}
export function traverse<V extends Kind>(
A: Applicative<V>,
): <A, I, J, K, L, M>(
favi: (a: A) => $<V, [I, J, K], [L], [M]>,
) => (ta: Datum<A>) => $<V, [Datum<I>, J, K], [L], [M]> {
return (favi) =>
match(
() => A.of(constInitial()),
() => A.of(constPending()),
(a) => pipe(favi(a), A.map((i) => replete(i))),
(a) => pipe(favi(a), A.map((i) => refresh(i))),
);
}
export function getShow<A>({ show }: Show<A>): Show<Datum<A>> {
return ({
show: match(
() => `Initial`,
() => `Pending`,
(a) => `Replete(${show(a)})`,
(a) => `Refresh(${show(a)})`,
),
});
}
export function getSemigroup<A>(
S: Semigroup<A>,
): Semigroup<Datum<A>> {
return ({
concat: (mx) =>
match(
() => mx,
() => toLoading(mx),
(v) =>
isSome(mx)
? (isRefresh(mx)
? refresh(S.concat(mx.value)(v))
: replete(S.concat(mx.value)(v)))
: (isPending(mx) ? refresh(v) : replete(v)),
(v) => isSome(mx) ? refresh(S.concat(mx.value)(v)) : refresh(v),
),
});
}
export function getMonoid<A>(S: Semigroup<A>): Monoid<Datum<A>> {
return ({
...getSemigroup(S),
empty: constInitial,
});
}
export function getEq<A>(S: Eq<A>): Eq<Datum<A>> {
return ({
equals: (b) =>
match(
() => isInitial(b),
() => isPending(b),
(v) => isReplete(b) ? S.equals(b.value)(v) : false,
(v) => isRefresh(b) ? S.equals(b.value)(v) : false,
),
});
}
export function getOrd<A>(O: Ord<A>): Ord<Datum<A>> {
return fromCompare((fst, snd) =>
pipe(
fst,
match(
() => isInitial(snd) ? 0 : -1,
() => isInitial(snd) ? 1 : isPending(snd) ? 0 : -1,
(value) =>
isNone(snd) ? 1 : isReplete(snd) ? O.compare(value, snd.value) : -1,
(value) => isRefresh(snd) ? O.compare(value, snd.value) : 1,
),
)
);
}
export const MonadDatum: Monad<KindDatum> = { of, ap, map, join, chain };
export const AltDatum: Alt<KindDatum> = { alt, map };
export const TraversableDatum: Traversable<KindDatum> = {
map,
reduce,
traverse,
};