-
Notifications
You must be signed in to change notification settings - Fork 437
/
Semigroup.kt
166 lines (130 loc) · 4.55 KB
/
Semigroup.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
package arrow.typeclasses
import arrow.core.Const
import arrow.core.Either
import arrow.core.Endo
import arrow.core.Ior
import arrow.core.NonEmptyList
import arrow.core.Option
import arrow.core.Validated
import arrow.core.combine
import arrow.core.compose
interface Semigroup<A> {
/**
* Combine two [A] values.
*/
fun A.combine(b: A): A
operator fun A.plus(b: A): A =
this.combine(b)
fun A.maybeCombine(b: A?): A =
b?.let { combine(it) } ?: this
companion object {
@JvmStatic
fun <A> list(): Semigroup<List<A>> = Monoid.list()
@JvmStatic
fun <A> sequence(): Semigroup<Sequence<A>> = Monoid.sequence()
@JvmStatic
fun string(): Semigroup<String> = Monoid.string()
@JvmStatic
@JvmName("Boolean")
fun boolean(): Semigroup<Boolean> = Monoid.boolean()
@JvmStatic
@JvmName("Byte")
fun byte(): Semigroup<Byte> = Monoid.byte()
@JvmStatic
@JvmName("Double")
fun double(): Semigroup<Double> = Monoid.double()
@JvmStatic
@JvmName("Integer")
fun int(): Semigroup<Int> = Monoid.int()
@JvmStatic
@JvmName("Long")
fun long(): Semigroup<Long> = Monoid.long()
@JvmStatic
@JvmName("Short")
fun short(): Semigroup<Short> = Monoid.short()
@JvmStatic
@JvmName("Float")
fun float(): Semigroup<Float> = Monoid.float()
@JvmStatic
fun <A, B> either(SA: Semigroup<A>, SB: Semigroup<B>): Semigroup<Either<A, B>> =
EitherSemigroup(SA, SB)
@JvmStatic
fun <A, B> ior(SA: Semigroup<A>, SB: Semigroup<B>): Semigroup<Ior<A, B>> =
IorSemigroup(SA, SB)
@JvmStatic
fun <A> endo(): Semigroup<Endo<A>> =
object : Semigroup<Endo<A>> {
override fun Endo<A>.combine(g: Endo<A>): Endo<A> = Endo(f.compose(g.f))
}
@JvmStatic
@JvmName("constant")
fun <A, T> const(SA: Semigroup<A>): Semigroup<Const<A, T>> =
object : Semigroup<Const<A, T>> {
override fun Const<A, T>.combine(b: Const<A, T>): Const<A, T> =
this.combine(SA, b)
}
@JvmStatic
fun <K, A> map(SG: Semigroup<A>): Semigroup<Map<K, A>> =
MapSemigroup(SG)
@JvmStatic
fun <A> option(SGA: Semigroup<A>): Semigroup<Option<A>> =
OptionSemigroup(SGA)
@JvmStatic
fun <E, A> validated(SE: Semigroup<E>, SA: Semigroup<A>): Semigroup<Validated<E, A>> =
ValidatedSemigroup(SE, SA)
@Suppress("UNCHECKED_CAST")
@JvmStatic
fun <A> nonEmptyList(): Semigroup<NonEmptyList<A>> =
NonEmptyListSemigroup as Semigroup<NonEmptyList<A>>
@JvmStatic
fun <A, B> pair(SA: Semigroup<A>, SB: Semigroup<B>): Semigroup<Pair<A, B>> =
PairSemigroup(SA, SB)
private open class PairSemigroup<A, B>(
private val SA: Semigroup<A>,
private val SB: Semigroup<B>
) : Semigroup<Pair<A, B>> {
override fun Pair<A, B>.combine(b: Pair<A, B>): Pair<A, B> = combine(SA, SB, b)
}
object NonEmptyListSemigroup : Semigroup<NonEmptyList<Any?>> {
override fun NonEmptyList<Any?>.combine(b: NonEmptyList<Any?>): NonEmptyList<Any?> =
NonEmptyList(this.head, this.tail.plus(b))
}
private open class ValidatedSemigroup<A, B>(
private val SA: Semigroup<A>,
private val SB: Semigroup<B>
) : Semigroup<Validated<A, B>> {
override fun Validated<A, B>.combine(b: Validated<A, B>): Validated<A, B> =
combine(SA, SB, b)
}
private class OptionSemigroup<A>(
private val SGA: Semigroup<A>
) : Semigroup<Option<A>> {
override fun Option<A>.combine(b: Option<A>): Option<A> =
combine(SGA, b)
override fun Option<A>.maybeCombine(b: Option<A>?): Option<A> =
b?.let { combine(SGA, it) } ?: this
}
private class MapSemigroup<K, A>(private val SG: Semigroup<A>) : Semigroup<Map<K, A>> {
override fun Map<K, A>.combine(b: Map<K, A>): Map<K, A> =
combine(SG, b)
}
private open class EitherSemigroup<L, R>(
private val SGL: Semigroup<L>,
private val SGR: Semigroup<R>
) : Semigroup<Either<L, R>> {
override fun Either<L, R>.combine(b: Either<L, R>): Either<L, R> =
combine(SGL, SGR, b)
override fun Either<L, R>.maybeCombine(b: Either<L, R>?): Either<L, R> =
b?.let { combine(SGL, SGR, it) } ?: this
}
private class IorSemigroup<A, B>(
private val SGA: Semigroup<A>,
private val SGB: Semigroup<B>
) : Semigroup<Ior<A, B>> {
override fun Ior<A, B>.combine(b: Ior<A, B>): Ior<A, B> =
combine(SGA, SGB, b)
override fun Ior<A, B>.maybeCombine(b: Ior<A, B>?): Ior<A, B> =
b?.let { combine(SGA, SGB, it) } ?: this
}
}
}