-
Notifications
You must be signed in to change notification settings - Fork 79
Gleam
Mappings from Gleam's result module and list module to kotlin-result.
- result.all
- result.flatten
- result.is_error
- result.is_ok
- result.lazy_or
- result.lazy_unwrap
- result.map
- result.map_error
- result.or
- result.partition
- result.replace
- result.replace_error
- result.try
- result.try_recover
- result.unwrap
- result.unwrap_error
- result.values
- list.try_each
- list.try_fold
- list.try_map
<V, E> Iterable<Result<V, E>>.combine(): Result<List<V>, E>
Combines a list of results into a single result. If all elements are Ok, returns Ok holding the list of values. If any element is Err, returns the first Err.
val values = combine(
Ok(20),
Ok(40),
Ok(60)
).get()
// values = [ 20, 40, 60 ]
val error = combine(
Ok(20),
Err(40),
Ok(60)
)
// error = Err(40)<V, E> Result<Result<V, E>, E>.flatten(): Result<V, E>
Merges a nested Result into a single layer.
val result = Ok(Ok("hello")).flatten()
// result = Ok("hello")
val result = Ok(Err(6)).flatten()
// result = Err(6)
val result = Err(6).flatten()
// result = Err(6)Checks whether the result is an Err value.
val result = Err(500)
if (result.isErr) {
println("Result is an error")
}Checks whether the result is an Ok value.
val result = Ok(500)
if (result.isOk) {
println("Result is ok")
}<V, E, F> Result<V, E>.orElse(transform: (E) -> Result<V, F>): Result<V, F>
Returns the first value if it is Ok, otherwise evaluates a function for a fallback result.
val result = Ok(1).orElse { Ok(2) }
// result = Ok(1)
val result = Err("error").orElse { Ok(2) }
// result = Ok(2)<V, E> Result<V, E>.getOrElse(transform: (E) -> V): V
Extracts the Ok value from a result, or evaluates a function for a fallback value.
val value = Ok(10).getOrElse { 0 }
// value = 10
val value = Err("error").getOrElse { 0 }
// value = 0<V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E>
Updates a value held within the Ok of a result by calling a given function on it.
val result = Ok(10).map { it + 20 }
// result = Ok(30)
val result = Err("error").map { it + 20 }
// result = Err("error")<V, E, F> Result<V, E>.mapError(transform: (E) -> F): Result<V, F>
Updates a value held within the Err of a result by calling a given function on it.
val result = Err(100).mapError { it * 5 }
// result = Err(500)
val result = Ok(100).mapError { it * 5 }
// result = Ok(100)<V, E, F> Result<V, E>.or(result: Result<V, F>): Result<V, F>
Returns the first value if it is Ok, otherwise returns the second value.
val result = Ok(1).or(Ok(2))
// result = Ok(1)
val result = Ok(1).or(Err("error"))
// result = Ok(1)
val result = Err("error").or(Ok(2))
// result = Ok(2)<V, E> Iterable<Result<V, E>>.partition(): Pair<List<V>, List<E>>
Partitions a list of results into a Pair of two lists — Ok values and Err values.
val pairs = partition(
Ok("one"),
Err("two"),
Ok("three"),
Err("four")
)
// pairs.first = [ "one", "three" ]
// pairs.second = [ "two", "four" ]<V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E>
Replaces the Ok value with a new value, leaving Err unchanged.
val result = Ok(1).map { 2 }
// result = Ok(2)
val result = Err("error").map { 2 }
// result = Err("error")<V, E, F> Result<V, E>.mapError(transform: (E) -> F): Result<V, F>
Replaces the Err value with a new value, leaving Ok unchanged.
val result = Err("error").mapError { "new error" }
// result = Err("new error")
val result = Ok(1).mapError { "new error" }
// result = Ok(1)<V, E, U> Result<V, E>.andThen(transform: (V) -> Result<U, E>): Result<U, E>
Chains together computations that may fail. If the result is Ok, the function is applied. If the result is an Err, it is returned unchanged.
Also available as flatMap.
val result = Ok(1).andThen { Ok(it * 2) }.andThen { Ok(it + 10) }
// result = Ok(12)
val result = Ok(1).andThen { Err("failed") }.andThen { Ok(it + 10) }
// result = Err("failed")<V, E, F> Result<V, E>.orElse(transform: (E) -> Result<V, F>): Result<V, F>
Calls transform if the result is Err, otherwise returns the Ok value of this.
val result = Err("error").orElse { Ok(1) }
// result = Ok(1)
val result = Ok(1).orElse { Ok(2) }
// result = Ok(1)<V, E> Result<V, E>.getOr(default: V): V
Extracts the Ok value from a result, returning a default value if the result is an Err.
val value = Ok(10).getOr(0)
// value = 10
val value = Err("error").getOr(0)
// value = 0<V, E> Result<V, E>.getErrorOr(default: E): E
Extracts the Err value from a result, returning a default value if the result is Ok.
val error = Err("error").getErrorOr("default")
// error = "error"
val error = Ok(10).getErrorOr("default")
// error = "default"<V, E> Iterable<Result<V, E>>.filterOk(): List<V>
Given a list of results, returns only the values inside Ok.
val values = listOf(Ok(1), Err("error"), Ok(3)).filterOk()
// values = [ 1, 3 ]<V, E> Iterable<V>.tryForEach(action: (V) -> Result<*, E>): Result<Unit, E>
Calls a function for each element, returning early with the first Err.
val result = listOf(1, 2, 3).tryForEach { Ok(println(it)) }
// result = Ok(Unit)
val result = listOf(1, 2, 3).tryForEach { if (it > 2) Err("too big") else Ok(Unit) }
// result = Err("too big")<T, R, E> Iterable<T>.tryFold(initial: R, operation: (acc: R, T) -> Result<R, E>): Result<R, E>
A fold over the elements of a list which can fail early.
val result = listOf(1, 2, 3).tryFold(0) { acc, x -> Ok(acc + x) }
// result = Ok(6)
val result = listOf(1, 2, 3).tryFold(0) { acc, x ->
if (x > 2) Err("too big") else Ok(acc + x)
}
// result = Err("too big")<V, E, U> Iterable<V>.tryMap(transform: (V) -> Result<U, E>): Result<List<U>, E>
Takes a function that returns a Result and maps it over a list, returning early with the first Err.
val result = listOf(1, 2, 3).tryMap { Ok(it * 2) }
// result = Ok([ 2, 4, 6 ])
val result = listOf(1, 2, 3).tryMap {
if (it > 2) Err("too big") else Ok(it * 2)
}
// result = Err("too big")