@@ -30,15 +30,22 @@ import com.fernandocejas.sample.core.functional.Either.Right
30
30
* @see Right
31
31
*/
32
32
sealed class Either <out L , out R > {
33
- /* * * Represents the left side of [Either] class which by convention is a "Failure". */
33
+
34
+ /* *
35
+ * Represents the left side of [Either] class which by convention
36
+ * is a "Failure".
37
+ */
34
38
data class Left <out L >
35
39
@Deprecated(" .toLeft()" , ReplaceWith (" a.toLeft()" ))
36
- constructor (val a : L ) : Either <L , Nothing >()
40
+ constructor (val left : L ) : Either <L , Nothing >()
37
41
38
- /* * * Represents the right side of [Either] class which by convention is a "Success". */
42
+ /* *
43
+ * Represents the right side of [Either] class which by convention
44
+ * is a "Success".
45
+ */
39
46
data class Right <out R >
40
47
@Deprecated(" .toRight()" , ReplaceWith (" b.toRight()" ))
41
- constructor (val b : R ) : Either <Nothing , R >()
48
+ constructor (val right : R ) : Either <Nothing , R >()
42
49
43
50
/* *
44
51
* Returns true if this is a Right, false otherwise.
@@ -59,8 +66,8 @@ sealed class Either<out L, out R> {
59
66
*/
60
67
fun <T > fold (fnL : (L ) -> T , fnR : (R ) -> T ): T =
61
68
when (this ) {
62
- is Left -> fnL(a )
63
- is Right -> fnR(b )
69
+ is Left -> fnL(left )
70
+ is Right -> fnR(right )
64
71
}
65
72
66
73
/* *
@@ -72,8 +79,8 @@ sealed class Either<out L, out R> {
72
79
*/
73
80
suspend fun <T > coFold (fnL : suspend (L ) -> T , fnR : suspend (R ) -> T ): T =
74
81
when (this ) {
75
- is Left -> fnL(a )
76
- is Right -> fnR(b )
82
+ is Left -> fnL(left )
83
+ is Right -> fnR(right )
77
84
}
78
85
79
86
companion object {
@@ -98,7 +105,7 @@ sealed class Either<out L, out R> {
98
105
fun <Left : Any , Right > Either <Exception , Right >.mapException (
99
106
operation : (Exception ) -> Left ?
100
107
): Either <Left , Right > = when (this ) {
101
- is Either .Left -> operation(a )?.toLeft() ? : throw a
108
+ is Either .Left -> operation(left )?.toLeft() ? : throw left
102
109
is Either .Right -> this
103
110
}
104
111
@@ -124,8 +131,8 @@ fun <A, B, C> ((A) -> B).c(f: (B) -> C): (A) -> C = {
124
131
*/
125
132
fun <T , L , R > Either <L , R >.flatMap (fn : (R ) -> Either <L , T >): Either <L , T > =
126
133
when (this ) {
127
- is Left -> Left (a )
128
- is Right -> fn(b )
134
+ is Left -> Left (left )
135
+ is Right -> fn(right )
129
136
}
130
137
131
138
/* *
@@ -135,8 +142,8 @@ fun <T, L, R> Either<L, R>.flatMap(fn: (R) -> Either<L, T>): Either<L, T> =
135
142
*/
136
143
suspend fun <T , L , R > Either <L , R >.coFlatMap (fn : suspend (R ) -> Either <L , T >): Either <L , T > =
137
144
when (this ) {
138
- is Left -> Left (a )
139
- is Right -> fn(b )
145
+ is Left -> Left (left )
146
+ is Right -> fn(right )
140
147
}
141
148
142
149
/* *
@@ -145,24 +152,24 @@ suspend fun <T, L, R> Either<L, R>.coFlatMap(fn: suspend (R) -> Either<L, T>): E
145
152
* object so you chain calls.
146
153
*/
147
154
infix fun <L , R > Either <L , R >.onFailure (fn : (failure: L ) -> Unit ): Either <L , R > =
148
- this .apply { if (this is Left ) fn(a ) }
155
+ this .apply { if (this is Left ) fn(left ) }
149
156
150
157
/* *
151
158
* Right-biased onSuccess() FP convention dictates that when this class is Right, it'll perform
152
159
* the onSuccess functionality passed as a parameter, but, overall will still return an either
153
160
* object so you chain calls.
154
161
*/
155
162
infix fun <L , R > Either <L , R >.onSuccess (fn : (success: R ) -> Unit ): Either <L , R > =
156
- this .apply { if (this is Right ) fn(b ) }
163
+ this .apply { if (this is Right ) fn(right ) }
157
164
158
165
/* *
159
166
* Right-biased map() FP convention which means that Right is assumed to be the default case
160
167
* to operate on. If it is Left, operations like map, flatMap, ... return the Left value unchanged.
161
168
*/
162
169
fun <L , R , T > Either <L , R >.map (fn : (R ) -> (T )): Either <L , T > =
163
170
when (this ) {
164
- is Left -> Left (a )
165
- is Right -> Right (fn(b ))
171
+ is Left -> Left (left )
172
+ is Right -> Right (fn(right ))
166
173
}
167
174
168
175
/* *
@@ -172,8 +179,8 @@ fun <L, R, T> Either<L, R>.map(fn: (R) -> (T)): Either<L, T> =
172
179
*/
173
180
suspend fun <L , R , T > Either <L , R >.coMap (fn : suspend (R ) -> (T )): Either <L , T > =
174
181
when (this ) {
175
- is Left -> Left (a )
176
- is Right -> Right (fn(b ))
182
+ is Left -> Left (left )
183
+ is Right -> Right (fn(right ))
177
184
}
178
185
179
186
/* *
@@ -183,5 +190,5 @@ suspend fun <L, R, T> Either<L, R>.coMap(fn: suspend (R) -> (T)): Either<L, T> =
183
190
fun <L , R > Either <L , R >.getOrElse (value : R ): R =
184
191
when (this ) {
185
192
is Left -> value
186
- is Right -> b
193
+ is Right -> right
187
194
}
0 commit comments