Add getErrorOr

Matches Haskell's Either.fromRight
This commit is contained in:
Michael Bull 2017-10-21 18:10:08 +01:00
parent eea45b3c88
commit be2fa210cc
6 changed files with 29 additions and 6 deletions

View File

@ -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 <V, E> Result<V, E>.and(result: Result<V, E>): Result<V, E> {
return when(this) {
return when (this) {
is Ok -> result
is Error -> err(error)
}

View File

@ -15,7 +15,7 @@ fun <V, E> Result<V, E>.get(): V? {
* - Rust: [Result.err](https://doc.rust-lang.org/std/result/enum.Result.html#method.err)
*/
fun <V, E> Result<V, E>.getError(): E? {
return when(this) {
return when (this) {
is Ok -> null
is Error -> error
}
@ -23,6 +23,7 @@ fun <V, E> Result<V, E>.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 <V, E> Result<V, E>.getOr(default: V): V {
@ -32,11 +33,21 @@ infix fun <V, E> Result<V, E>.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 <V, E> Result<V, E>.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 <V,E> Result<V,E>.getOrElse(transform: (E) -> V): V {
infix inline fun <V, E> Result<V, E>.getOrElse(transform: (E) -> V): V {
return when (this) {
is Ok -> value
is Error -> transform(error)

View File

@ -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 <V, E> Result<V, E>.or(result: Result<V, E>): Result<V, E> {
return when(this) {
return when (this) {
is Ok -> ok(value)
is Error -> result
}

View File

@ -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)

View File

@ -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" }

View File

@ -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 {