From be2fa210cc4bcb07ad6f636475d963887e9128d1 Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Sat, 21 Oct 2017 18:10:08 +0100 Subject: [PATCH] Add getErrorOr Matches Haskell's Either.fromRight --- src/main/kotlin/com/mikebull94/result/And.kt | 2 +- src/main/kotlin/com/mikebull94/result/Get.kt | 15 +++++++++++++-- src/main/kotlin/com/mikebull94/result/Or.kt | 2 +- src/main/kotlin/com/mikebull94/result/Unwrap.kt | 2 +- src/test/kotlin/com/mikebull94/result/GetTest.kt | 12 ++++++++++++ .../com/mikebull94/result/ResultIteratorTest.kt | 2 +- 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/mikebull94/result/And.kt b/src/main/kotlin/com/mikebull94/result/And.kt index c05f7d8..d0f5613 100644 --- a/src/main/kotlin/com/mikebull94/result/And.kt +++ b/src/main/kotlin/com/mikebull94/result/And.kt @@ -4,7 +4,7 @@ package com.mikebull94.result * Rust: [Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and) */ infix fun Result.and(result: Result): Result { - return when(this) { + return when (this) { is Ok -> result is Error -> err(error) } diff --git a/src/main/kotlin/com/mikebull94/result/Get.kt b/src/main/kotlin/com/mikebull94/result/Get.kt index 5387022..8f7146b 100644 --- a/src/main/kotlin/com/mikebull94/result/Get.kt +++ b/src/main/kotlin/com/mikebull94/result/Get.kt @@ -15,7 +15,7 @@ fun Result.get(): V? { * - Rust: [Result.err](https://doc.rust-lang.org/std/result/enum.Result.html#method.err) */ fun Result.getError(): E? { - return when(this) { + return when (this) { is Ok -> null is Error -> error } @@ -23,6 +23,7 @@ fun Result.getError(): E? { /** * - Elm: [Result.withDefault](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#withDefault) + * - Haskell: [Result.fromLeft](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:fromLeft) * - Rust: [Result.unwrap_or](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or) */ infix fun Result.getOr(default: V): V { @@ -32,11 +33,21 @@ infix fun Result.getOr(default: V): V { } } +/** + * - Haskell: [Result.fromRight](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:fromRight) + */ +infix fun Result.getErrorOr(default: E): E { + return when (this) { + is Ok -> default + is Error -> error + } +} + /** * - Elm: [Result.extract](http://package.elm-lang.org/packages/circuithub/elm-result-extra/1.4.0/Result-Extra#extract) * - Rust: [Result.unwrap_or_else](https://doc.rust-lang.org/src/core/result.rs.html#735-740) */ -infix inline fun Result.getOrElse(transform: (E) -> V): V { +infix inline fun Result.getOrElse(transform: (E) -> V): V { return when (this) { is Ok -> value is Error -> transform(error) diff --git a/src/main/kotlin/com/mikebull94/result/Or.kt b/src/main/kotlin/com/mikebull94/result/Or.kt index a5c134c..b929da3 100644 --- a/src/main/kotlin/com/mikebull94/result/Or.kt +++ b/src/main/kotlin/com/mikebull94/result/Or.kt @@ -4,7 +4,7 @@ package com.mikebull94.result * - Rust: [Result.or](https://doc.rust-lang.org/std/result/enum.Result.html#method.or) */ infix fun Result.or(result: Result): Result { - return when(this) { + return when (this) { is Ok -> ok(value) is Error -> result } diff --git a/src/main/kotlin/com/mikebull94/result/Unwrap.kt b/src/main/kotlin/com/mikebull94/result/Unwrap.kt index 16333d0..e5b9cb8 100644 --- a/src/main/kotlin/com/mikebull94/result/Unwrap.kt +++ b/src/main/kotlin/com/mikebull94/result/Unwrap.kt @@ -1,6 +1,6 @@ package com.mikebull94.result -class UnwrapException(message: String): Exception(message) +class UnwrapException(message: String) : Exception(message) /** * - Rust: [Result.unwrap](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap) diff --git a/src/test/kotlin/com/mikebull94/result/GetTest.kt b/src/test/kotlin/com/mikebull94/result/GetTest.kt index 856ba5f..b7bf5a4 100644 --- a/src/test/kotlin/com/mikebull94/result/GetTest.kt +++ b/src/test/kotlin/com/mikebull94/result/GetTest.kt @@ -43,6 +43,18 @@ internal class GetTest { assertThat(value, equalTo("default")) } + @Test + internal fun `getErrorOr should return the default value if ok`() { + val error = ok("hello").getErrorOr("world") + assertThat(error, equalTo("world")) + } + + @Test + internal fun `getErrorOr should return the result error if not ok`() { + val error = err("hello").getErrorOr("world") + assertThat(error, equalTo("hello")) + } + @Test internal fun `getOrElse should return the result value if ok`() { val value = ok("hello").getOrElse { "world" } diff --git a/src/test/kotlin/com/mikebull94/result/ResultIteratorTest.kt b/src/test/kotlin/com/mikebull94/result/ResultIteratorTest.kt index 72763bb..4f789c3 100644 --- a/src/test/kotlin/com/mikebull94/result/ResultIteratorTest.kt +++ b/src/test/kotlin/com/mikebull94/result/ResultIteratorTest.kt @@ -2,7 +2,7 @@ package com.mikebull94.result import com.natpryce.hamkrest.assertion.assertThat import com.natpryce.hamkrest.equalTo -import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Test internal class ResultIteratorTest {