-
Notifications
You must be signed in to change notification settings - Fork 448
/
Every.kt
323 lines (291 loc) · 11 KB
/
Every.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
package arrow.optics
import arrow.core.Either
import arrow.core.NonEmptyList
import arrow.core.Option
import arrow.core.Tuple4
import arrow.core.Tuple5
import arrow.core.Tuple6
import arrow.core.Tuple7
import arrow.core.Tuple8
import arrow.core.Tuple9
import arrow.core.foldLeft
import arrow.core.foldMap
import arrow.typeclasses.Monoid
import kotlin.jvm.JvmStatic
public object Every {
/**
* [Traversal] for [List] that focuses in each [A] of the source [List].
*/
@JvmStatic
public fun <A> list(): Traversal<List<A>, A> =
object : Traversal<List<A>, A> {
override fun modify(source: List<A>, map: (focus: A) -> A): List<A> =
source.map(map)
override fun <R> foldMap(M: Monoid<R>, source: List<A>, map: (focus: A) -> R): R =
source.foldMap(M, map)
}
/**
* [Traversal] for [Either] that has focus in each [Either.Right].
*
* @receiver [PTraversal.Companion] to make it statically available.
* @return [Traversal] with source [Either] and focus every [Either.Right] of the source.
*/
@JvmStatic
public fun <L, R> either(): Traversal<Either<L, R>, R> =
object : Traversal<Either<L, R>, R> {
override fun modify(source: Either<L, R>, map: (focus: R) -> R): Either<L, R> =
source.map(map)
override fun <A> foldMap(M: Monoid<A>, source: Either<L, R>, map: (focus: R) -> A): A =
when (source) {
is Either.Left<L> -> M.empty()
is Either.Right<R> -> map(source.value)
}
}
@JvmStatic
public fun <K, V> map(): Traversal<Map<K, V>, V> =
object : Traversal<Map<K, V>, V> {
override fun modify(source: Map<K, V>, map: (focus: V) -> V): Map<K, V> =
source.mapValues { (_, v) -> map(v) }
override fun <R> foldMap(M: Monoid<R>, source: Map<K, V>, map: (focus: V) -> R): R =
M.run {
source.foldLeft(empty()) { acc, (_, v) ->
acc.combine(map(v))
}
}
}
/**
* [Traversal] for [NonEmptyList] that has focus in each [A].
*
* @receiver [PTraversal.Companion] to make it statically available.
* @return [Traversal] with source [NonEmptyList] and focus every [A] of the source.
*/
@JvmStatic
public fun <A> nonEmptyList(): Traversal<NonEmptyList<A>, A> =
object : Traversal<NonEmptyList<A>, A> {
override fun modify(source: NonEmptyList<A>, map: (focus: A) -> A): NonEmptyList<A> =
source.map(map)
override fun <R> foldMap(M: Monoid<R>, source: NonEmptyList<A>, map: (focus: A) -> R): R =
source.foldMap(M, map)
}
/**
* [Traversal] for [Option] that has focus in each [arrow.core.Some].
*
* @receiver [PTraversal.Companion] to make it statically available.
* @return [Traversal] with source [Option] and focus in every [arrow.core.Some] of the source.
*/
@JvmStatic
public fun <A> option(): Traversal<Option<A>, A> =
object : Traversal<Option<A>, A> {
override fun modify(source: Option<A>, map: (focus: A) -> A): Option<A> =
source.map(map)
override fun <R> foldMap(M: Monoid<R>, source: Option<A>, map: (focus: A) -> R): R =
source.foldMap(M, map)
}
@JvmStatic
public fun <A> sequence(): Traversal<Sequence<A>, A> =
object : Traversal<Sequence<A>, A> {
override fun modify(source: Sequence<A>, map: (focus: A) -> A): Sequence<A> =
source.map(map)
override fun <R> foldMap(M: Monoid<R>, source: Sequence<A>, map: (focus: A) -> R): R =
M.run {
source.fold(empty()) { acc, a ->
acc.combine(map(a))
}
}
}
/**
* [Traversal] for [String] that focuses in each [Char] of the source [String].
*
* @receiver [PTraversal.Companion] to make it statically available.
* @return [Traversal] with source [String] and foci every [Char] in the source.
*/
@JvmStatic
public fun string(): Traversal<String, Char> =
object : Traversal<String, Char> {
override fun modify(source: String, map: (focus: Char) -> Char): String =
source.map(map).joinToString(separator = "")
override fun <R> foldMap(M: Monoid<R>, source: String, map: (focus: Char) -> R): R =
M.run {
source.fold(empty()) { acc, char -> acc.combine(map(char)) }
}
}
/**
* [Traversal] to focus into the first and second value of a [Pair]
*/
@JvmStatic
public fun <A> pair(): Traversal<Pair<A, A>, A> =
object : Traversal<Pair<A, A>, A> {
override fun modify(source: Pair<A, A>, map: (focus: A) -> A): Pair<A, A> =
Pair(map(source.first), map(source.second))
override fun <R> foldMap(M: Monoid<R>, source: Pair<A, A>, map: (focus: A) -> R): R =
M.run {
map(source.first).combine(map(source.second))
}
}
/**
* [Traversal] to focus into the first, second and third value of a [Triple]
*/
@JvmStatic
public fun <A> triple(): Traversal<Triple<A, A, A>, A> =
object : Traversal<Triple<A, A, A>, A> {
override fun modify(source: Triple<A, A, A>, map: (focus: A) -> A): Triple<A, A, A> =
Triple(map(source.first), map(source.second), map(source.third))
override fun <R> foldMap(M: Monoid<R>, source: Triple<A, A, A>, map: (focus: A) -> R): R =
M.run {
map(source.first)
.combine(map(source.second))
.combine(map(source.third))
}
}
/**
* [Traversal] to focus into the first, second, third and fourth value of a [arrow.core.Tuple4]
*/
@JvmStatic
public fun <A> tuple4(): Traversal<Tuple4<A, A, A, A>, A> =
object : Traversal<Tuple4<A, A, A, A>, A> {
override fun modify(source: Tuple4<A, A, A, A>, map: (focus: A) -> A): Tuple4<A, A, A, A> =
Tuple4(map(source.first), map(source.second), map(source.third), map(source.fourth))
override fun <R> foldMap(M: Monoid<R>, source: Tuple4<A, A, A, A>, map: (focus: A) -> R): R =
M.run {
map(source.first)
.combine(map(source.second))
.combine(map(source.third))
.combine(map(source.fourth))
}
}
/**
* [PTraversal] to focus into the first, second, third, fourth and fifth value of a [arrow.core.Tuple5]
*/
@JvmStatic
public fun <A> tuple5(): Traversal<Tuple5<A, A, A, A, A>, A> =
object : Traversal<Tuple5<A, A, A, A, A>, A> {
override fun modify(source: Tuple5<A, A, A, A, A>, map: (focus: A) -> A): Tuple5<A, A, A, A, A> =
Tuple5(map(source.first), map(source.second), map(source.third), map(source.fourth), map(source.fifth))
override fun <R> foldMap(M: Monoid<R>, source: Tuple5<A, A, A, A, A>, map: (focus: A) -> R): R =
M.run {
map(source.first)
.combine(map(source.second))
.combine(map(source.third))
.combine(map(source.fourth))
.combine(map(source.fifth))
}
}
/**
* [Traversal] to focus into the first, second, third, fourth, fifth and sixth value of a [arrow.core.Tuple6]
*/
@JvmStatic
public fun <A> tuple6(): Traversal<Tuple6<A, A, A, A, A, A>, A> =
object : Traversal<Tuple6<A, A, A, A, A, A>, A> {
override fun modify(source: Tuple6<A, A, A, A, A, A>, map: (focus: A) -> A): Tuple6<A, A, A, A, A, A> =
Tuple6(
map(source.first),
map(source.second),
map(source.third),
map(source.fourth),
map(source.fifth),
map(source.sixth)
)
override fun <R> foldMap(M: Monoid<R>, source: Tuple6<A, A, A, A, A, A>, map: (focus: A) -> R): R =
M.run {
map(source.first)
.combine(map(source.second))
.combine(map(source.third))
.combine(map(source.fourth))
.combine(map(source.fifth))
.combine(map(source.sixth))
}
}
/**
* [Traversal] to focus into the first, second, third, fourth, fifth, sixth and seventh value of a [arrow.core.Tuple7]
*/
@JvmStatic
public fun <A> tuple7(): Traversal<Tuple7<A, A, A, A, A, A, A>, A> =
object : Traversal<Tuple7<A, A, A, A, A, A, A>, A> {
override fun modify(source: Tuple7<A, A, A, A, A, A, A>, map: (focus: A) -> A): Tuple7<A, A, A, A, A, A, A> =
Tuple7(
map(source.first),
map(source.second),
map(source.third),
map(source.fourth),
map(source.fifth),
map(source.sixth),
map(source.seventh)
)
override fun <R> foldMap(M: Monoid<R>, source: Tuple7<A, A, A, A, A, A, A>, map: (focus: A) -> R): R =
M.run {
map(source.first)
.combine(map(source.second))
.combine(map(source.third))
.combine(map(source.fourth))
.combine(map(source.fifth))
.combine(map(source.sixth))
.combine(map(source.seventh))
}
}
/**
* [Traversal] to focus into the first, second, third, fourth, fifth, sixth, seventh and eight value of a [arrow.core.Tuple8]
*/
@JvmStatic
public fun <A> tuple8(): Traversal<Tuple8<A, A, A, A, A, A, A, A>, A> =
object : Traversal<Tuple8<A, A, A, A, A, A, A, A>, A> {
override fun modify(
source: Tuple8<A, A, A, A, A, A, A, A>,
map: (focus: A) -> A
): Tuple8<A, A, A, A, A, A, A, A> =
Tuple8(
map(source.first),
map(source.second),
map(source.third),
map(source.fourth),
map(source.fifth),
map(source.sixth),
map(source.seventh),
map(source.eighth)
)
override fun <R> foldMap(M: Monoid<R>, source: Tuple8<A, A, A, A, A, A, A, A>, map: (focus: A) -> R): R =
M.run {
map(source.first)
.combine(map(source.second))
.combine(map(source.third))
.combine(map(source.fourth))
.combine(map(source.fifth))
.combine(map(source.sixth))
.combine(map(source.seventh))
.combine(map(source.eighth))
}
}
/**
* [Traversal] to focus into the first, second, third, fourth, fifth, sixth, seventh, eight and ninth value of a [arrow.core.Tuple9]
*/
@JvmStatic
public fun <A> tuple9(): Traversal<Tuple9<A, A, A, A, A, A, A, A, A>, A> =
object : Traversal<Tuple9<A, A, A, A, A, A, A, A, A>, A> {
override fun modify(
source: Tuple9<A, A, A, A, A, A, A, A, A>,
map: (focus: A) -> A
): Tuple9<A, A, A, A, A, A, A, A, A> =
Tuple9(
map(source.first),
map(source.second),
map(source.third),
map(source.fourth),
map(source.fifth),
map(source.sixth),
map(source.seventh),
map(source.eighth),
map(source.ninth)
)
override fun <R> foldMap(M: Monoid<R>, source: Tuple9<A, A, A, A, A, A, A, A, A>, map: (focus: A) -> R): R =
M.run {
map(source.first)
.combine(map(source.second))
.combine(map(source.third))
.combine(map(source.fourth))
.combine(map(source.fifth))
.combine(map(source.sixth))
.combine(map(source.seventh))
.combine(map(source.eighth))
.combine(map(source.ninth))
}
}
}