Add getErrorOr
Matches Haskell's Either.fromRight
This commit is contained in:
parent
eea45b3c88
commit
be2fa210cc
@ -4,7 +4,7 @@ package com.mikebull94.result
|
|||||||
* Rust: [Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and)
|
* 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> {
|
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 Ok -> result
|
||||||
is Error -> err(error)
|
is Error -> err(error)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
* - Rust: [Result.err](https://doc.rust-lang.org/std/result/enum.Result.html#method.err)
|
||||||
*/
|
*/
|
||||||
fun <V, E> Result<V, E>.getError(): E? {
|
fun <V, E> Result<V, E>.getError(): E? {
|
||||||
return when(this) {
|
return when (this) {
|
||||||
is Ok -> null
|
is Ok -> null
|
||||||
is Error -> error
|
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)
|
* - 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)
|
* - 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 {
|
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)
|
* - 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)
|
* - 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) {
|
return when (this) {
|
||||||
is Ok -> value
|
is Ok -> value
|
||||||
is Error -> transform(error)
|
is Error -> transform(error)
|
||||||
|
@ -4,7 +4,7 @@ package com.mikebull94.result
|
|||||||
* - Rust: [Result.or](https://doc.rust-lang.org/std/result/enum.Result.html#method.or)
|
* - 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> {
|
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 Ok -> ok(value)
|
||||||
is Error -> result
|
is Error -> result
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.mikebull94.result
|
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)
|
* - Rust: [Result.unwrap](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap)
|
||||||
|
@ -43,6 +43,18 @@ internal class GetTest {
|
|||||||
assertThat(value, equalTo("default"))
|
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
|
@Test
|
||||||
internal fun `getOrElse should return the result value if ok`() {
|
internal fun `getOrElse should return the result value if ok`() {
|
||||||
val value = ok("hello").getOrElse { "world" }
|
val value = ok("hello").getOrElse { "world" }
|
||||||
|
@ -2,7 +2,7 @@ package com.mikebull94.result
|
|||||||
|
|
||||||
import com.natpryce.hamkrest.assertion.assertThat
|
import com.natpryce.hamkrest.assertion.assertThat
|
||||||
import com.natpryce.hamkrest.equalTo
|
import com.natpryce.hamkrest.equalTo
|
||||||
import org.junit.jupiter.api.Assertions.*
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
internal class ResultIteratorTest {
|
internal class ResultIteratorTest {
|
||||||
|
Loading…
Reference in New Issue
Block a user