1
1
package kategory
2
2
3
+ typealias Right <A , B > = Either .Right <A , B >
4
+ typealias Left <A , B > = Either .Left <A , B >
5
+
3
6
/* *
4
7
* Port of https://github.com/scala/scala/blob/v2.12.1/src/library/scala/util/Either.scala
5
8
*
@@ -44,16 +47,16 @@ package kategory
44
47
fun <C > foldL (b : C , f : (C , B ) -> C ): C =
45
48
this .ev().let { either ->
46
49
when (either) {
47
- is Either . Right -> f(b, either.b)
48
- is Either . Left -> b
50
+ is Right -> f(b, either.b)
51
+ is Left -> b
49
52
}
50
53
}
51
54
52
55
fun <C > foldR (lb : Eval <C >, f : (B , Eval <C >) -> Eval <C >): Eval <C > =
53
56
this .ev().let { either ->
54
57
when (either) {
55
- is Either . Right -> f(either.b, lb)
56
- is Either . Left -> lb
58
+ is Right -> f(either.b, lb)
59
+ is Left -> lb
57
60
}
58
61
}
59
62
@@ -109,7 +112,7 @@ package kategory
109
112
* Left(12).toOption() // Result: None
110
113
* ```
111
114
*/
112
- fun toOption (): Option <B > = fold({ Option . None }, { Option . Some (it) })
115
+ fun toOption (): Option <B > = fold({ None }, { Some (it) })
113
116
114
117
/* *
115
118
* The left side of the disjoint union, as opposed to the [Right] side.
@@ -140,12 +143,12 @@ package kategory
140
143
tailrec fun <L , A , B > tailRecM (a : A , f : (A ) -> HK <EitherKindPartial <L >, Either <A , B >>): Either <L , B > {
141
144
val ev: Either <L , Either <A , B >> = f(a).ev()
142
145
return when (ev) {
143
- is Either . Left <L , Either <A , B >> -> ev.a.left()
144
- is Either . Right <L , Either <A , B >> -> {
146
+ is Left <L , Either <A , B >> -> ev.a.left()
147
+ is Right <L , Either <A , B >> -> {
145
148
val b: Either <A , B > = ev.b
146
149
when (b) {
147
- is Either . Left <A , B > -> tailRecM(b.a, f)
148
- is Either . Right <A , B > -> b.b.right()
150
+ is Left <A , B > -> tailRecM(b.a, f)
151
+ is Right <A , B > -> b.b.right()
149
152
}
150
153
}
151
154
}
@@ -159,7 +162,7 @@ package kategory
159
162
*
160
163
* @param f The function to bind across [Either.Right].
161
164
*/
162
- inline fun <A , B , C > Either <A , B >.flatMap (crossinline f : (B ) -> Either <A , C >): Either <A , C > = fold({ Either . Left (it) }, { f(it) })
165
+ inline fun <A , B , C > Either <A , B >.flatMap (crossinline f : (B ) -> Either <A , C >): Either <A , C > = fold({ Left (it) }, { f(it) })
163
166
164
167
/* *
165
168
* Returns the value from this [Either.Right] or the given argument if this is a [Either.Left].
@@ -189,7 +192,7 @@ inline fun <B> Either<*, B>.getOrElse(crossinline default: () -> B): B = fold({
189
192
* ```
190
193
*/
191
194
inline fun <A , B > Either <A , B >.filterOrElse (crossinline predicate : (B ) -> Boolean , crossinline default : () -> A ): Either <A , B > =
192
- fold({ Either . Left (it) }, { if (predicate(it)) Either . Right (it) else Either . Left (default()) })
195
+ fold({ Left (it) }, { if (predicate(it)) Right (it) else Left (default()) })
193
196
194
197
/* *
195
198
* Returns `true` if this is a [Either.Right] and its value is equal to `elem` (as determined by `==`),
@@ -210,15 +213,15 @@ fun <A, B> Either<A, B>.contains(elem: B): Boolean = fold({ false }, { it == ele
210
213
fun <A , B , C > Either <A , B >.ap (ff : EitherKind <A , (B ) - > C >): Either <A , C > = ff.flatMap { f -> map(f) }.ev()
211
214
212
215
fun <G , A , B , C > Either <A , B >.traverse (f : (B ) -> HK <G , C >, GA : Applicative <G >): HK <G , Either <A , C >> =
213
- this .ev().fold({ GA .pure(it.left()) }, { GA .map(f(it), { Either . Right (it) }) })
216
+ this .ev().fold({ GA .pure(it.left()) }, { GA .map(f(it), { Right (it) }) })
214
217
215
218
fun <A , B > Either <A , B >.combineK (y : EitherKind <A , B >): Either <A , B > =
216
219
when (this ) {
217
- is Either . Left -> y.ev()
220
+ is Left -> y.ev()
218
221
else -> this .ev()
219
222
}
220
223
221
- fun <A > A.left (): Either <A , Nothing > = Either . Left (this )
224
+ fun <A > A.left (): Either <A , Nothing > = Left (this )
222
225
223
- fun <A > A.right (): Either <Nothing , A > = Either . Right (this )
226
+ fun <A > A.right (): Either <Nothing , A > = Right (this )
224
227
0 commit comments