This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap.ts
277 lines (244 loc) · 6.78 KB
/
map.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
import type * as HKT from "./hkt.ts";
import type * as TC from "./type_classes.ts";
import * as O from "./option.ts";
import * as A from "./array.ts";
import { compare } from "./ord.ts";
import { fromEquals } from "./setoid.ts";
import { constant, identity, pipe } from "./fns.ts";
/*******************************************************************************
* Kind Registration
******************************************************************************/
export const URI = "Map";
export type URI = typeof URI;
declare module "./hkt.ts" {
// deno-lint-ignore no-explicit-any
export interface Kinds<_ extends any[]> {
[URI]: Map<_[1], _[0]>;
}
}
/*******************************************************************************
* Constructors
******************************************************************************/
export const zero: Map<never, never> = new Map<never, never>();
export const empty = <K, A>(): Map<K, A> => new Map<K, A>();
export const singleton = <K, A>(k: K, a: A): Map<K, A> => new Map([[k, a]]);
/*******************************************************************************
* Modules
******************************************************************************/
export const Functor: TC.Functor<URI> = {
map: (fab) =>
(ta) => {
const tb = new Map();
for (const [k, a] of ta) {
tb.set(k, fab(a));
}
return tb;
},
};
export const Bifunctor: TC.Bifunctor<URI> = {
bimap: (fab, fcd) =>
(tac) => {
const tbd = new Map();
for (const [a, c] of tac) {
tbd.set(fab(a), fcd(c));
}
return tbd;
},
mapLeft: (fef) => (ta) => pipe(ta, Bifunctor.bimap(fef, identity)),
};
/*******************************************************************************
* Module Getters
******************************************************************************/
export const getShow = <K, A>(
SK: TC.Show<K>,
SA: TC.Show<A>,
): TC.Show<Map<K, A>> => ({
show: (ta) => {
const elements = Array.from(ta).map(([k, a]) =>
`[${SK.show(k)}, ${SA.show(a)}]`
).join(", ");
return `new Map([${elements}])`;
},
});
export const getSetoid = <K, A>(
SK: TC.Setoid<K>,
SA: TC.Setoid<A>,
): TC.Setoid<Map<K, A>> => {
const submap = isSubmap(SK, SA);
return fromEquals((x) => (y) => submap(x)(y) && submap(y)(x));
};
export const getMonoid = <K, A>(
SK: TC.Setoid<K>,
SA: TC.Semigroup<A>,
): TC.Monoid<Map<K, A>> => {
const lookupKey = lookupWithKey(SK);
return {
concat: (a) =>
(b) => {
if (a === zero) {
return b;
}
if (b === zero) {
return a;
}
const r = new Map(a);
for (const [bk, ba] of b) {
pipe(
lookupKey(bk)(a),
O.fold(
() => r.set(bk, ba),
([ak, aa]) => r.set(ak, SA.concat(aa)(ba)),
),
);
}
return r;
},
empty,
};
};
/*******************************************************************************
* Pipeables
******************************************************************************/
export const size = <K, A>(d: Map<K, A>): number => d.size;
export const isEmpty = <K, A>(d: Map<K, A>): boolean => d.size === 0;
export const lookupWithKey = <K>(S: TC.Setoid<K>) =>
(k: K) =>
<A>(m: Map<K, A>): O.Option<readonly [K, A]> => {
for (const [ka, a] of m.entries()) {
if (S.equals(ka)(k)) {
return O.some([ka, a]);
}
}
return O.none;
};
export const lookup = <K>(S: TC.Setoid<K>) =>
(k: K) =>
<A>(m: Map<K, A>): O.Option<A> => {
const lookupWithKeyE = lookupWithKey(S);
return pipe(
lookupWithKeyE(k)(m),
O.map(([k, a]) => a),
);
};
export const member = <K>(S: TC.Setoid<K>) => {
const lookupKey = lookup(S);
return (k: K) =>
<A>(m: Map<K, A>): boolean => pipe(lookupKey(k)(m), O.isSome);
};
export const elem = <A>(S: TC.Setoid<A>) =>
(a: A) =>
<K>(m: Map<K, A>): boolean => {
for (const ma of m.values()) {
if (S.equals(ma)(a)) {
return true;
}
}
return false;
};
export const keys = <K>(O: TC.Ord<K>) =>
<A>(m: Map<K, A>): readonly K[] => Array.from(m.keys()).sort(compare(O));
export const values = <A>(O: TC.Ord<A>) =>
<K>(m: Map<K, A>): readonly A[] => Array.from(m.values()).sort(compare(O));
export const collect = <K>(O: TC.Ord<K>) => {
const getKeys = keys(O);
return <A, B>(f: (k: K, a: A) => B) =>
(m: Map<K, A>): readonly B[] =>
pipe(
getKeys(m),
A.map((k) => f(k, m.get(k)!)),
);
};
export const insertAt = <K>(S: TC.Setoid<K>) => {
const lookupKey = lookupWithKey(S);
return <A>(k: K, a: A) =>
(m: Map<K, A>): Map<K, A> => {
const found = lookupKey(k)(m);
if (O.isNone(found)) {
const r = new Map(m);
r.set(k, a);
return r;
} else if (found.value[1] !== a) {
const r = new Map(m);
r.set(found.value[0], a);
return r;
}
return m;
};
};
export const deleteAt = <K>(
S: TC.Setoid<K>,
) =>
(k: K) => {
const lookupIn = lookupWithKey(S)(k);
return <A>(m: Map<K, A>): Map<K, A> => {
const found = lookupIn(m);
if (O.isSome(found)) {
const r = new Map(m);
r.delete(found.value[0]);
return r;
}
return m;
};
};
export const updateAt = <K>(
S: TC.Setoid<K>,
) =>
<A>(k: K, a: A) => {
const lookupIn = lookupWithKey(S)(k);
return (m: Map<K, A>): O.Option<Map<K, A>> => {
const found = lookupIn(m);
if (O.isNone(found)) {
return O.none;
}
const r = new Map(m);
r.set(found.value[0], a);
return O.some(r);
};
};
export const modifyAt = <K>(
S: TC.Setoid<K>,
) =>
<A>(
k: K,
f: (a: A) => A,
) => {
const lookupIn = lookupWithKey(S)(k);
return (m: Map<K, A>): O.Option<Map<K, A>> => {
const found = lookupIn(m);
if (O.isNone(found)) {
return O.none;
}
const r = new Map(m);
r.set(found.value[0], f(found.value[1]));
return O.some(r);
};
};
export const pop = <K>(S: TC.Setoid<K>) =>
(k: K) => {
const lookupIn = lookup(S)(k);
const deleteIn = deleteAt(S)(k);
return <A>(m: Map<K, A>): O.Option<readonly [A, Map<K, A>]> =>
pipe(
lookupIn(m),
O.map((a) => [a, deleteIn(m)]),
);
};
export const isSubmap = <K, A>(
SK: TC.Setoid<K>,
SA: TC.Setoid<A>,
) =>
(sub: Map<K, A>) => {
const lookupKey = lookupWithKey(SK);
return (sup: Map<K, A>): boolean => {
for (const [mk, ma] of sub.entries()) {
const matches = pipe(
lookupKey(mk)(sup),
O.exists(([_, ca]) => SA.equals(ma)(ca)),
);
if (!matches) {
return false;
}
}
return true;
};
};