Skip to content

Commit dac8d1c

Browse files
coughlacCorina Usher
and
Corina Usher
authoredJul 3, 2024
Add bimap and fold to Result (#64)
* Add bimap to Result Signed-off-by: Corina Usher <coughlac@hotmail.com> Signed-off-by: Corina Usher <corina.usher@example.com> * Add fold to Result Signed-off-by: Corina Usher <coughlac@hotmail.com> Signed-off-by: Corina Usher <corina.usher@example.com> --------- Signed-off-by: Corina Usher <coughlac@hotmail.com> Signed-off-by: Corina Usher <corina.usher@example.com> Co-authored-by: Corina Usher <corina.usher@example.com>
1 parent 6ec09be commit dac8d1c

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
 

‎result4k/core/src/main/kotlin/dev/forkhandles/result4k/result.kt

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ inline fun <T, Tʹ, E> Result<T, E>.flatMap(f: (T) -> Result<Tʹ, E>): Result<T
5151
inline fun <T, E, Eʹ> Result<T, E>.mapFailure(f: (E) -> Eʹ): Result<T, Eʹ> =
5252
flatMapFailure { reason -> Failure(f(reason)) }
5353

54+
/**
55+
* Map a function, f, over the `value` of a successful `Result`
56+
* and a function, g, over the `reason` of an unsuccessful `Result`.
57+
*/
58+
inline fun <T, Tʹ, E, Eʹ> Result<T, E>.bimap(f: (T) -> Tʹ, g: (E) -> Eʹ): Result<Tʹ, Eʹ> =
59+
map { f(it) }.mapFailure { g(it) }
60+
61+
/**
62+
* Fold a function, f, over the `value` of a successful `Result`
63+
* and a function, g, over the `reason` of an unsuccessful `Result`
64+
* where both functions result in a value of the same type, returning a plain value.
65+
*/
66+
inline fun <T, E, Tʹ> Result<T, E>.fold(f: (T) -> Tʹ, g: (E) -> Tʹ): Tʹ =
67+
bimap(f, g).get()
68+
5469
/**
5570
* Flat-map a function over the `reason` of an unsuccessful `Result`.
5671
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dev.forkhandles.result4k
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.fail
6+
7+
class BiMapTests {
8+
@Test
9+
fun `bimap success`() {
10+
val result = Success(123).bimap(
11+
{ "This is the successful result: ${it + 1}" },
12+
{ fail("should not be called for a success") })
13+
14+
assertEquals(Success("This is the successful result: 124"), result)
15+
}
16+
17+
@Test
18+
fun `bimap failure`() {
19+
val result = Failure(123).bimap(
20+
{ fail("should not be called for a failure") },
21+
{ "This is the failed result: ${it + 1}" })
22+
23+
assertEquals(Failure("This is the failed result: 124"), result)
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dev.forkhandles.result4k
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.fail
6+
7+
class FoldTests {
8+
@Test
9+
fun `fold success`() {
10+
val result = Success(123).fold({ "A value of: ${(it + 1)}" }, { fail("should not be called for a success") })
11+
assertEquals("A value of: 124", result)
12+
}
13+
14+
@Test
15+
fun `fold failure`() {
16+
val result = Failure(123).fold({ fail("should not be called for a failure") }, { "A value of: ${(it + 1)}" })
17+
assertEquals("A value of: 124", result)
18+
}
19+
}

0 commit comments

Comments
 (0)