Skip to content

Commit ac1f54a

Browse files
committed
ref: better param names for Either.kt file
1 parent 9f33527 commit ac1f54a

File tree

1 file changed

+27
-20
lines changed
  • app/src/main/kotlin/com/fernandocejas/sample/core/functional

1 file changed

+27
-20
lines changed

app/src/main/kotlin/com/fernandocejas/sample/core/functional/Either.kt

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,22 @@ import com.fernandocejas.sample.core.functional.Either.Right
3030
* @see Right
3131
*/
3232
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+
*/
3438
data class Left<out L>
3539
@Deprecated(".toLeft()", ReplaceWith("a.toLeft()"))
36-
constructor(val a: L) : Either<L, Nothing>()
40+
constructor(val left: L) : Either<L, Nothing>()
3741

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+
*/
3946
data class Right<out R>
4047
@Deprecated(".toRight()", ReplaceWith("b.toRight()"))
41-
constructor(val b: R) : Either<Nothing, R>()
48+
constructor(val right: R) : Either<Nothing, R>()
4249

4350
/**
4451
* Returns true if this is a Right, false otherwise.
@@ -59,8 +66,8 @@ sealed class Either<out L, out R> {
5966
*/
6067
fun <T> fold(fnL: (L) -> T, fnR: (R) -> T): T =
6168
when (this) {
62-
is Left -> fnL(a)
63-
is Right -> fnR(b)
69+
is Left -> fnL(left)
70+
is Right -> fnR(right)
6471
}
6572

6673
/**
@@ -72,8 +79,8 @@ sealed class Either<out L, out R> {
7279
*/
7380
suspend fun <T> coFold(fnL: suspend (L) -> T, fnR: suspend (R) -> T): T =
7481
when (this) {
75-
is Left -> fnL(a)
76-
is Right -> fnR(b)
82+
is Left -> fnL(left)
83+
is Right -> fnR(right)
7784
}
7885

7986
companion object {
@@ -98,7 +105,7 @@ sealed class Either<out L, out R> {
98105
fun <Left : Any, Right> Either<Exception, Right>.mapException(
99106
operation: (Exception) -> Left?
100107
): Either<Left, Right> = when (this) {
101-
is Either.Left -> operation(a)?.toLeft() ?: throw a
108+
is Either.Left -> operation(left)?.toLeft() ?: throw left
102109
is Either.Right -> this
103110
}
104111

@@ -124,8 +131,8 @@ fun <A, B, C> ((A) -> B).c(f: (B) -> C): (A) -> C = {
124131
*/
125132
fun <T, L, R> Either<L, R>.flatMap(fn: (R) -> Either<L, T>): Either<L, T> =
126133
when (this) {
127-
is Left -> Left(a)
128-
is Right -> fn(b)
134+
is Left -> Left(left)
135+
is Right -> fn(right)
129136
}
130137

131138
/**
@@ -135,8 +142,8 @@ fun <T, L, R> Either<L, R>.flatMap(fn: (R) -> Either<L, T>): Either<L, T> =
135142
*/
136143
suspend fun <T, L, R> Either<L, R>.coFlatMap(fn: suspend (R) -> Either<L, T>): Either<L, T> =
137144
when (this) {
138-
is Left -> Left(a)
139-
is Right -> fn(b)
145+
is Left -> Left(left)
146+
is Right -> fn(right)
140147
}
141148

142149
/**
@@ -145,24 +152,24 @@ suspend fun <T, L, R> Either<L, R>.coFlatMap(fn: suspend (R) -> Either<L, T>): E
145152
* object so you chain calls.
146153
*/
147154
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) }
149156

150157
/**
151158
* Right-biased onSuccess() FP convention dictates that when this class is Right, it'll perform
152159
* the onSuccess functionality passed as a parameter, but, overall will still return an either
153160
* object so you chain calls.
154161
*/
155162
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) }
157164

158165
/**
159166
* Right-biased map() FP convention which means that Right is assumed to be the default case
160167
* to operate on. If it is Left, operations like map, flatMap, ... return the Left value unchanged.
161168
*/
162169
fun <L, R, T> Either<L, R>.map(fn: (R) -> (T)): Either<L, T> =
163170
when (this) {
164-
is Left -> Left(a)
165-
is Right -> Right(fn(b))
171+
is Left -> Left(left)
172+
is Right -> Right(fn(right))
166173
}
167174

168175
/**
@@ -172,8 +179,8 @@ fun <L, R, T> Either<L, R>.map(fn: (R) -> (T)): Either<L, T> =
172179
*/
173180
suspend fun <L, R, T> Either<L, R>.coMap(fn: suspend (R) -> (T)): Either<L, T> =
174181
when (this) {
175-
is Left -> Left(a)
176-
is Right -> Right(fn(b))
182+
is Left -> Left(left)
183+
is Right -> Right(fn(right))
177184
}
178185

179186
/**
@@ -183,5 +190,5 @@ suspend fun <L, R, T> Either<L, R>.coMap(fn: suspend (R) -> (T)): Either<L, T> =
183190
fun <L, R> Either<L, R>.getOrElse(value: R): R =
184191
when (this) {
185192
is Left -> value
186-
is Right -> b
193+
is Right -> right
187194
}

0 commit comments

Comments
 (0)