Skip to content

Commit

Permalink
Add Result.toOutcome (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
Synesso authored Jun 21, 2024
1 parent cf3250d commit 2069bbd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added
* Adds `Result<T>.unit(): Result<Unit>` as alias for `.map { }` (Jem Mawson)
* Adds `Result<T>.tap` and `Result<T>.flatTap` (Jem Mawson)
* Adds `Result<T>.toOutcome` (Jem Mawson)

## [0.5.5] - 2024-06-20

Expand Down
1 change: 1 addition & 0 deletions lib/api/lib.api
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public final class app/cash/quiver/OutcomeKt {
public static final fun tapFailure (Lapp/cash/quiver/Outcome;Lkotlin/jvm/functions/Function1;)Lapp/cash/quiver/Outcome;
public static final fun toOutcome (Larrow/core/Either;)Lapp/cash/quiver/Outcome;
public static final fun toOutcome (Larrow/core/Option;)Lapp/cash/quiver/Outcome;
public static final fun toOutcome (Ljava/lang/Object;)Lapp/cash/quiver/Outcome;
public static final fun traverse (Lapp/cash/quiver/Outcome;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun traverse (Lapp/cash/quiver/Outcome;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public static final fun traverse (Lapp/cash/quiver/Outcome;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/main/kotlin/app/cash/quiver/Outcome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ fun <E, A> Either<E, Option<A>>.toOutcome(): Outcome<E, A> = when (this) {

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

fun <A> Result<A>.toOutcome(): Outcome<Throwable, A> = fold(
onSuccess = { Present(it) },
onFailure = { Failure(it) }
)

fun <A> Option<A>.toOutcome(): Outcome<Nothing, A> = this.right().toOutcome()

inline fun <A> Outcome<Throwable, A>.orThrow(onAbsent: () -> Throwable): A = when (this) {
Expand Down
7 changes: 7 additions & 0 deletions lib/src/test/kotlin/app/cash/quiver/OutcomeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import arrow.core.right
import arrow.core.some
import arrow.core.valid
import app.cash.quiver.continuations.outcome
import app.cash.quiver.extensions.success
import io.kotest.assertions.arrow.core.shouldBeInvalid
import io.kotest.assertions.arrow.core.shouldBeLeft
import io.kotest.assertions.arrow.core.shouldBeNone
Expand Down Expand Up @@ -190,6 +191,12 @@ class OutcomeTest : StringSpec({
}.shouldBeAbsent()
}

"Lift Result<A> into Outcome" {
val e = RuntimeException("hey")
Result.success(1).toOutcome().shouldBePresent().shouldBe(1)
Result.failure<Int>(e).toOutcome().shouldBeFailure().shouldBe(e)
}

"Lift Either<E,A> into Outcome" {
1.right().asOutcome().shouldBePresent().shouldBe(1)
"bad".left().asOutcome().shouldBeFailure().shouldBe("bad")
Expand Down

0 comments on commit 2069bbd

Please sign in to comment.