Skip to content

Commit 2069bbd

Browse files
authored
Add Result.toOutcome (#91)
1 parent cf3250d commit 2069bbd

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66
* Adds `Result<T>.unit(): Result<Unit>` as alias for `.map { }` (Jem Mawson)
77
* Adds `Result<T>.tap` and `Result<T>.flatTap` (Jem Mawson)
8+
* Adds `Result<T>.toOutcome` (Jem Mawson)
89

910
## [0.5.5] - 2024-06-20
1011

lib/api/lib.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public final class app/cash/quiver/OutcomeKt {
6060
public static final fun tapFailure (Lapp/cash/quiver/Outcome;Lkotlin/jvm/functions/Function1;)Lapp/cash/quiver/Outcome;
6161
public static final fun toOutcome (Larrow/core/Either;)Lapp/cash/quiver/Outcome;
6262
public static final fun toOutcome (Larrow/core/Option;)Lapp/cash/quiver/Outcome;
63+
public static final fun toOutcome (Ljava/lang/Object;)Lapp/cash/quiver/Outcome;
6364
public static final fun traverse (Lapp/cash/quiver/Outcome;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
6465
public static final fun traverse (Lapp/cash/quiver/Outcome;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
6566
public static final fun traverse (Lapp/cash/quiver/Outcome;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;

lib/src/main/kotlin/app/cash/quiver/Outcome.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ fun <E, A> Either<E, Option<A>>.toOutcome(): Outcome<E, A> = when (this) {
217217

218218
fun <E, A> Either<E, A>.asOutcome(): Outcome<E, A> = this.map(::Some).toOutcome()
219219

220+
fun <A> Result<A>.toOutcome(): Outcome<Throwable, A> = fold(
221+
onSuccess = { Present(it) },
222+
onFailure = { Failure(it) }
223+
)
224+
220225
fun <A> Option<A>.toOutcome(): Outcome<Nothing, A> = this.right().toOutcome()
221226

222227
inline fun <A> Outcome<Throwable, A>.orThrow(onAbsent: () -> Throwable): A = when (this) {

lib/src/test/kotlin/app/cash/quiver/OutcomeTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import arrow.core.right
1717
import arrow.core.some
1818
import arrow.core.valid
1919
import app.cash.quiver.continuations.outcome
20+
import app.cash.quiver.extensions.success
2021
import io.kotest.assertions.arrow.core.shouldBeInvalid
2122
import io.kotest.assertions.arrow.core.shouldBeLeft
2223
import io.kotest.assertions.arrow.core.shouldBeNone
@@ -190,6 +191,12 @@ class OutcomeTest : StringSpec({
190191
}.shouldBeAbsent()
191192
}
192193

194+
"Lift Result<A> into Outcome" {
195+
val e = RuntimeException("hey")
196+
Result.success(1).toOutcome().shouldBePresent().shouldBe(1)
197+
Result.failure<Int>(e).toOutcome().shouldBeFailure().shouldBe(e)
198+
}
199+
193200
"Lift Either<E,A> into Outcome" {
194201
1.right().asOutcome().shouldBePresent().shouldBe(1)
195202
"bad".left().asOutcome().shouldBeFailure().shouldBe("bad")

0 commit comments

Comments
 (0)