/
map.kt
493 lines (452 loc) · 14.4 KB
/
map.kt
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
@file:OptIn(ExperimentalTypeInference::class)
package arrow.core
import arrow.core.raise.either
import arrow.core.raise.mapOrAccumulate
import arrow.core.raise.RaiseAccumulate
import kotlin.experimental.ExperimentalTypeInference
/**
* Combines to structures by taking the intersection of their shapes
* and using `Pair` to hold the elements.
*
* ```kotlin
* import arrow.core.zip
* import io.kotest.matchers.shouldBe
*
* fun test() {
* mapOf(1 to "A", 2 to "B")
* .zip(mapOf(1 to "1", 2 to "2", 3 to "3")) shouldBe mapOf(1 to Pair("A", "1"), 2 to Pair("B", "2"))
* }
* ```
* <!--- KNIT example-map-01.kt -->
* <!--- lines.isEmpty() -->
*/
public fun <K, A, B> Map<K, A>.zip(other: Map<K, B>): Map<K, Pair<A, B>> =
zip(other) { _, a, b -> Pair(a, b) }
/**
* Combines to structures by taking the intersection of their shapes
* and combining the elements with the given function.
*
* ```kotlin
* import arrow.core.zip
* import io.kotest.matchers.shouldBe
*
* fun test() {
* mapOf(1 to "A", 2 to "B").zip(mapOf(1 to "1", 2 to "2", 3 to "3")) {
* _, a, b -> "$a ~ $b"
* } shouldBe mapOf(1 to "A ~ 1", 2 to "B ~ 2")
* }
* ```
* <!--- KNIT example-map-02.kt -->
* <!--- lines.isEmpty() -->
*/
@Suppress("UNCHECKED_CAST")
public inline fun <Key, A, B, C> Map<Key, A>.zip(other: Map<Key, B>, map: (Key, A, B) -> C): Map<Key, C> =
buildMap(size) {
this@zip.forEach { (key, bb) ->
if (other.containsKey(key)) {
put(key, map(key, bb, other[key] as B))
}
}
}
@Suppress("UNCHECKED_CAST")
public inline fun <Key, B, C, D, E> Map<Key, B>.zip(
c: Map<Key, C>,
d: Map<Key, D>,
map: (Key, B, C, D) -> E
): Map<Key, E> = buildMap(size) {
this@zip.forEach { (key, bb) ->
if (c.containsKey(key) && d.containsKey(key)) {
val cc = c[key] as C
val dd = d[key] as D
put(key, map(key, bb, cc, dd))
}
}
}
@Suppress("UNCHECKED_CAST")
public inline fun <Key, B, C, D, E, F> Map<Key, B>.zip(
c: Map<Key, C>,
d: Map<Key, D>,
e: Map<Key, E>,
map: (Key, B, C, D, E) -> F
): Map<Key, F> = buildMap(size) {
this@zip.forEach { (key, bb) ->
if (c.containsKey(key) && d.containsKey(key) && e.containsKey(key)) {
val cc = c[key] as C
val dd = d[key] as D
val ee = e[key] as E
put(key, map(key, bb, cc, dd, ee))
}
}
}
@Suppress("UNCHECKED_CAST")
public inline fun <Key, B, C, D, E, F, G> Map<Key, B>.zip(
c: Map<Key, C>,
d: Map<Key, D>,
e: Map<Key, E>,
f: Map<Key, F>,
map: (Key, B, C, D, E, F) -> G
): Map<Key, G> = buildMap(size) {
this@zip.forEach { (key, bb) ->
if (c.containsKey(key) && d.containsKey(key) && e.containsKey(key) && f.containsKey(key)) {
val cc = c[key] as C
val dd = d[key] as D
val ee = e[key] as E
val ff = f[key] as F
put(key, map(key, bb, cc, dd, ee, ff))
}
}
}
@Suppress("UNCHECKED_CAST")
public inline fun <Key, B, C, D, E, F, G, H> Map<Key, B>.zip(
c: Map<Key, C>,
d: Map<Key, D>,
e: Map<Key, E>,
f: Map<Key, F>,
g: Map<Key, G>,
map: (Key, B, C, D, E, F, G) -> H
): Map<Key, H> = buildMap(size) {
this@zip.forEach { (key, bb) ->
if (c.containsKey(key) && d.containsKey(key) && e.containsKey(key) && f.containsKey(key) && g.containsKey(key)) {
val cc = c[key] as C
val dd = d[key] as D
val ee = e[key] as E
val ff = f[key] as F
val gg = g[key] as G
put(key, map(key, bb, cc, dd, ee, ff, gg))
}
}
}
@Suppress("UNCHECKED_CAST")
public inline fun <Key, B, C, D, E, F, G, H, I> Map<Key, B>.zip(
c: Map<Key, C>,
d: Map<Key, D>,
e: Map<Key, E>,
f: Map<Key, F>,
g: Map<Key, G>,
h: Map<Key, H>,
map: (Key, B, C, D, E, F, G, H) -> I
): Map<Key, I> = buildMap(size) {
this@zip.forEach { (key, bb) ->
if (c.containsKey(key) && d.containsKey(key) && e.containsKey(key) && f.containsKey(key) && g.containsKey(key) && h.containsKey(key)) {
val cc = c[key] as C
val dd = d[key] as D
val ee = e[key] as E
val ff = f[key] as F
val gg = g[key] as G
val hh = h[key] as H
put(key, map(key, bb, cc, dd, ee, ff, gg, hh))
}
}
}
@Suppress("UNCHECKED_CAST")
public inline fun <Key, B, C, D, E, F, G, H, I, J> Map<Key, B>.zip(
c: Map<Key, C>,
d: Map<Key, D>,
e: Map<Key, E>,
f: Map<Key, F>,
g: Map<Key, G>,
h: Map<Key, H>,
i: Map<Key, I>,
map: (Key, B, C, D, E, F, G, H, I) -> J
): Map<Key, J> = buildMap(size) {
this@zip.forEach { (key, bb) ->
if (c.containsKey(key) && d.containsKey(key) && e.containsKey(key) && f.containsKey(key) && g.containsKey(key) && h.containsKey(key) && i.containsKey(key)) {
val cc = c[key] as C
val dd = d[key] as D
val ee = e[key] as E
val ff = f[key] as F
val gg = g[key] as G
val hh = h[key] as H
val ii = i[key] as I
put(
key, map(key, bb, cc, dd, ee, ff, gg, hh, ii)
)
}
}
}
@Suppress("UNCHECKED_CAST")
public inline fun <Key, B, C, D, E, F, G, H, I, J, K> Map<Key, B>.zip(
c: Map<Key, C>,
d: Map<Key, D>,
e: Map<Key, E>,
f: Map<Key, F>,
g: Map<Key, G>,
h: Map<Key, H>,
i: Map<Key, I>,
j: Map<Key, J>,
map: (Key, B, C, D, E, F, G, H, I, J) -> K
): Map<Key, K> = buildMap(size) {
this@zip.forEach { (key, bb) ->
if (c.containsKey(key) && d.containsKey(key) && e.containsKey(key) && f.containsKey(key) && g.containsKey(key) && h.containsKey(key) && i.containsKey(key) && j.containsKey(key)) {
val cc = c[key] as C
val dd = d[key] as D
val ee = e[key] as E
val ff = f[key] as F
val gg = g[key] as G
val hh = h[key] as H
val ii = i[key] as I
val jj = j[key] as J
put(key, map(key, bb, cc, dd, ee, ff, gg, hh, ii, jj))
}
}
}
@Suppress("UNCHECKED_CAST")
public inline fun <Key, B, C, D, E, F, G, H, I, J, K, L> Map<Key, B>.zip(
c: Map<Key, C>,
d: Map<Key, D>,
e: Map<Key, E>,
f: Map<Key, F>,
g: Map<Key, G>,
h: Map<Key, H>,
i: Map<Key, I>,
j: Map<Key, J>,
k: Map<Key, K>,
map: (Key, B, C, D, E, F, G, H, I, J, K) -> L
): Map<Key, L> = buildMap(size) {
this@zip.forEach { (key, bb) ->
if (c.containsKey(key) && d.containsKey(key) && e.containsKey(key) && f.containsKey(key) && g.containsKey(key) && h.containsKey(key) && i.containsKey(key) && j.containsKey(key) && k.containsKey(key)) {
val cc = c[key] as C
val dd = d[key] as D
val ee = e[key] as E
val ff = f[key] as F
val gg = g[key] as G
val hh = h[key] as H
val ii = i[key] as I
val jj = j[key] as J
val kk = k[key] as K
put(key, map(key, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk))
}
}
}
/**
* Transform every [Map.Entry] of the original [Map] using [f],
* only keeping the [Map.Entry] of the transformed map that match the input [Map.Entry].
*/
@Suppress("UNCHECKED_CAST")
public fun <K, A, B> Map<K, A>.flatMap(f: (Map.Entry<K, A>) -> Map<K, B>): Map<K, B> =
buildMap {
this@flatMap.forEach { entry ->
val nestedMap = f(entry)
if (nestedMap.containsKey(entry.key)) {
put(entry.key, nestedMap[entry.key] as B)
}
}
}
public inline fun <K, E, A, B> Map<K, A>.mapOrAccumulate(
combine: (E, E) -> E,
@BuilderInference transform: RaiseAccumulate<E>.(Map.Entry<K, A>) -> B
): Either<E, Map<K, B>> = either {
mapOrAccumulate(this@mapOrAccumulate, combine, transform)
}
public inline fun <K, E, A, B> Map<K, A>.mapOrAccumulate(
@BuilderInference transform: RaiseAccumulate<E>.(Map.Entry<K, A>) -> B
): Either<NonEmptyList<E>, Map<K, B>> = either {
mapOrAccumulate(this@mapOrAccumulate, transform)
}
public fun <K, A, B> Map<K, A>.mapNotNull(transform: (Map.Entry<K, A>) -> B?): Map<K, B> =
buildMap {
this@mapNotNull.forEach { entry ->
transform(entry)?.let { put(entry.key, it) }
}
}
public fun <K, A> Map<K, Option<A>>.filterOption(): Map<K, A> =
buildMap {
this@filterOption.forEach { (key, option) ->
option.fold({ }, { put(key, it) })
}
}
/**
* Returns a Map containing all elements that are instances of specified type parameter R.
*/
@Suppress("UNCHECKED_CAST")
public inline fun <K, reified R> Map<K, *>.filterIsInstance(): Map<K, R> =
filterValues { it is R } as Map<K, R>
/**
* Combines two structures by taking the union of their shapes and using Ior to hold the elements.
*
* ```kotlin
* import arrow.core.*
* import io.kotest.matchers.shouldBe
*
* fun test() {
* val res = mapOf(1 to 1, 2 to 2).align(mapOf(1 to "1", 2 to "2", 3 to "3"))
* res shouldBe mapOf(1 to Ior.Both(1, "1"), 2 to Ior.Both(2, "2"), 3 to Ior.Right("3"))
* }
* ```
* <!--- KNIT example-map-03.kt -->
* <!--- lines.isEmpty() -->
*/
public fun <K, A, B> Map<K, A>.align(b: Map<K, B>): Map<K, Ior<A, B>> =
padZip(b, { _, a -> Ior.Left(a) }, { _, bb -> Ior.Right(bb) }) { _, a, bb -> Ior.Both(a, bb) }
/**
* Combines two structures by taking the union of their shapes and combining the elements with the given function.
*
* ```kotlin
* import arrow.core.*
* import io.kotest.matchers.shouldBe
*
* fun test() {
* mapOf("1" to 1, "2" to 2)
* .align(mapOf("1" to 1, "2" to 2, "3" to 3)) { (_, a) ->
* "$a"
* } shouldBe mapOf("1" to "Ior.Both(1, 1)", "2" to Ior.Both(2, 2), "3" to Ior.Right(3))
* }
* ```
* <!--- KNIT example-map-04.kt -->
* <!--- lines.isEmpty() -->
*/
public fun <K, A, B, C> Map<K, A>.align(b: Map<K, B>, fa: (Map.Entry<K, Ior<A, B>>) -> C): Map<K, C> =
padZip(
b,
{ k, a -> fa(Entry(k, Ior.Left(a))) },
{ k, bb -> fa(Entry(k, Ior.Right(bb))) }
) { k, a, bb -> fa(Entry(k, Ior.Both(a, bb))) }
private class Entry<K, V>(override val key: K, override val value: V) : Map.Entry<K, V> {
override fun hashCode(): Int = key.hashCode() xor value.hashCode()
override fun toString(): String = "$key=$value"
override fun equals(other: Any?): Boolean =
other is Map.Entry<*, *> && other.key == key && other.value == value
}
public fun <K, A> Map<K, A>.salign(other: Map<K, A>, combine: (A, A) -> A): Map<K, A> =
padZip(other, { _, a -> a }, { _, b -> b }) { _, a, b -> combine(a, b) }
/**
* Align two structures as in zip, but filling in blanks with null.
*/
public fun <K, A, B> Map<K, A>.padZip(other: Map<K, B>): Map<K, Pair<A?, B?>> =
padZip(other) { _, a, b -> a to b }
public fun <K, A, B, C> Map<K, A>.padZip(other: Map<K, B>, fa: (K, A?, B?) -> C): Map<K, C> =
padZip(other, { k, a -> fa(k, a, null) }, { k, b -> fa(k, null, b) }) { k, a, b -> fa(k, a, b) }
@Suppress("UNCHECKED_CAST")
public inline fun <K, A, B, C> Map<K, A>.padZip(
other: Map<K, B>,
left: (K, A) -> C,
right: (K, B) -> C,
both: (K, A, B) -> C
): Map<K, C> = buildMap {
(this@padZip.keys + other.keys).forEach { key ->
when {
this@padZip.containsKey(key) && other.containsKey(key) ->
put(key, both(key, this@padZip[key] as A, other[key] as B))
this@padZip.containsKey(key) -> put(key, left(key, this@padZip[key] as A))
other.containsKey(key) -> put(key, right(key, other[key] as B))
}
}
}
/**
* Splits a union into its component parts.
*
* ```kotlin
* import arrow.core.*
* import io.kotest.matchers.shouldBe
*
* fun test() {
* mapOf(
* "first" to Ior.Both("A", 1),
* "second" to Ior.Both("B", 2),
* "third" to Ior.Left("C")
* ).unalign() shouldBe Pair(mapOf("first" to "A", "second" to "B", "third" to "C"), mapOf("first" to 1, "second" to 2))
* }
* ```
* <!--- KNIT example-map-05.kt -->
* <!--- lines.isEmpty() -->
*/
public fun <K, A, B> Map<K, Ior<A, B>>.unalign(): Pair<Map<K, A>, Map<K, B>> =
unalign { (_, ior) -> ior }
/**
* after applying the given function, splits the resulting union shaped structure into its components parts
*
* ```kotlin
* import arrow.core.*
* import io.kotest.matchers.shouldBe
*
* fun test() {
* mapOf("1" to 1, "2" to 2, "3" to 3)
* .unalign { (key, value) ->
* when(key) {
* "1" -> Ior.Left(value)
* "2" -> Ior.Right(key)
* else -> Ior.Both(value, key)
* }
* } shouldBe Pair(mapOf("1" to 1, "3" to 3), mapOf("2" to 2, "3" to 3))
* }
* ```
* <!--- KNIT example-map-06.kt -->
* <!--- lines.isEmpty() -->
*/
public inline fun <K, A, B, C> Map<K, C>.unalign(fa: (Map.Entry<K, C>) -> Ior<A, B>): Pair<Map<K, A>, Map<K, B>> {
val lefts = mutableMapOf<K, A>()
val rights = mutableMapOf<K, B>()
forEach { entry ->
fa(entry).fold(
{ lefts[entry.key] = it },
{ rights[entry.key] = it },
{ a, b ->
lefts[entry.key] = a
rights[entry.key] = b
}
)
}
return lefts to rights
}
/**
* Unzips the structure holding the resulting elements in an `Pair`
*
* ```kotlin
* import arrow.core.*
* import io.kotest.matchers.shouldBe
*
* fun test() {
* mapOf(
* "first" to ("A" to 1),
* "second" to ("B" to 2)
* ).unzip() shouldBe Pair(mapOf("first" to "A", "second" to "B"), mapOf("first" to 1, "second" to 2))
* }
* ```
* <!--- KNIT example-map-07.kt -->
* <!--- lines.isEmpty() -->
*/
public fun <K, A, B> Map<K, Pair<A, B>>.unzip(): Pair<Map<K, A>, Map<K, B>> =
unzip { (_, pair) -> pair }
/**
* After applying the given function unzip the resulting structure into its elements.
*
* ```kotlin
* import arrow.core.*
* import io.kotest.matchers.shouldBe
*
* fun test() {
* mapOf("first" to "A:1", "second" to "B:2", "third" to "C:3").unzip { (_, e) ->
* e.split(":").let {
* it.first() to it.last()
* }
* } shouldBe Pair(
* mapOf("first" to "A", "second" to "B", "third" to "C"),
* mapOf("first" to "1", "second" to "2", "third" to "3")
* )
* }
* ```
* <!--- KNIT example-map-08.kt -->
* <!--- lines.isEmpty() -->
*/
public inline fun <K, A, B, C> Map<K, C>.unzip(fc: (Map.Entry<K, C>) -> Pair<A, B>): Pair<Map<K, A>, Map<K, B>> {
val lefts = mutableMapOf<K, A>()
val rights = mutableMapOf<K, B>()
forEach { entry ->
val (a, b) = fc(entry)
lefts[entry.key] = a
rights[entry.key] = b
}
return lefts to rights
}
@Suppress("UNCHECKED_CAST")
public fun <K, V> Map<K, V>.getOrNone(key: K): Option<V> =
if (containsKey(key)) Some(get(key) as V) else None
/** Combines two maps using [combine] to combine values for the same key. */
public fun <K, A> Map<K, A>.combine(other: Map<K, A>, combine: (A, A) -> A): Map<K, A> =
if (size < other.size) fold(other) { my, (k, b) -> my + Pair(k, my[k]?.let { combine(b, it) } ?: b) }
else other.fold(this@combine) { my, (k, a) -> my + Pair(k, my[k]?.let { combine(a, it) } ?: a) }
public inline fun <K, A, B> Map<K, A>.fold(initial: B, operation: (acc: B, Map.Entry<K, A>) -> B): B {
var accumulator = initial
forEach { accumulator = operation(accumulator, it) }
return accumulator
}