2017-10-21 23:59:16 +00:00
|
|
|
package com.github.michaelbull.result
|
2017-10-21 02:51:30 +00:00
|
|
|
|
2017-10-21 20:19:21 +00:00
|
|
|
/**
|
|
|
|
* Accumulates value starting with [initial] value and applying [operation] from left to right to
|
|
|
|
* current accumulator value and each element.
|
|
|
|
*
|
|
|
|
* @param initial The value to start with.
|
|
|
|
* @param operation The operation to apply to each element and current accumulator value.
|
|
|
|
*/
|
2017-10-21 02:51:30 +00:00
|
|
|
inline fun <T, R, E> Iterable<T>.fold(
|
|
|
|
initial: R,
|
|
|
|
operation: (acc: R, T) -> Result<R, E>
|
|
|
|
): Result<R, E> {
|
|
|
|
var accumulator = initial
|
|
|
|
|
|
|
|
forEach { element ->
|
|
|
|
val operationResult = operation(accumulator, element)
|
|
|
|
|
|
|
|
when (operationResult) {
|
|
|
|
is Ok -> {
|
|
|
|
accumulator = operationResult.value
|
|
|
|
}
|
2017-10-22 14:05:02 +00:00
|
|
|
is Error -> return Error(operationResult.error)
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 14:05:02 +00:00
|
|
|
return Ok(accumulator)
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 20:19:21 +00:00
|
|
|
/**
|
|
|
|
* Accumulates value starting with [initial] value and applying [operation] from right to left to
|
|
|
|
* each element and current accumulator value.
|
|
|
|
*
|
|
|
|
* @param initial The value to start with.
|
|
|
|
* @param operation The operation to apply to each element and current accumulator value.
|
|
|
|
*/
|
2017-10-21 03:21:56 +00:00
|
|
|
inline fun <T, R, E> List<T>.foldRight(
|
|
|
|
initial: R,
|
|
|
|
operation: (T, acc: R) -> Result<R, E>
|
|
|
|
): Result<R, E> {
|
2017-10-21 02:51:30 +00:00
|
|
|
var accumulator = initial
|
|
|
|
|
|
|
|
if (!isEmpty()) {
|
|
|
|
val iterator = listIterator(size)
|
|
|
|
while (iterator.hasPrevious()) {
|
|
|
|
val operationResult = operation(iterator.previous(), accumulator)
|
|
|
|
|
|
|
|
when (operationResult) {
|
|
|
|
is Ok -> {
|
|
|
|
accumulator = operationResult.value
|
|
|
|
}
|
2017-10-22 14:05:02 +00:00
|
|
|
is Error -> return Error(operationResult.error)
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 14:05:02 +00:00
|
|
|
return Ok(accumulator)
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-21 20:19:21 +00:00
|
|
|
* Combine a vararg of [Results][Result] into a single [Result] (holding a [List]).
|
|
|
|
*
|
2017-10-21 02:51:30 +00:00
|
|
|
* - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/circuithub/elm-result-extra/1.4.0/Result-Extra#combine)
|
2017-10-21 20:19:21 +00:00
|
|
|
*
|
|
|
|
* @param results The [Results][Result] to combine.
|
|
|
|
* @return The combined [Result].
|
2017-10-21 02:51:30 +00:00
|
|
|
*/
|
|
|
|
fun <V, E> combine(vararg results: Result<V, E>) = results.asIterable().combine()
|
|
|
|
|
|
|
|
/**
|
2017-10-21 20:19:21 +00:00
|
|
|
* Combine an [Iterable] of [Results][Result] into a single [Result] (holding a [List]).
|
|
|
|
*
|
2017-10-21 02:51:30 +00:00
|
|
|
* - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/circuithub/elm-result-extra/1.4.0/Result-Extra#combine)
|
2017-10-21 20:19:21 +00:00
|
|
|
*
|
|
|
|
* @return The combined [Result].
|
2017-10-21 02:51:30 +00:00
|
|
|
*/
|
|
|
|
fun <V, E> Iterable<Result<V, E>>.combine(): Result<List<V>, E> {
|
2017-10-22 14:05:02 +00:00
|
|
|
return Ok(map {
|
2017-10-21 02:51:30 +00:00
|
|
|
when (it) {
|
|
|
|
is Ok -> it.value
|
2017-10-22 14:05:02 +00:00
|
|
|
is Error -> return it
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-21 20:19:21 +00:00
|
|
|
* Extracts from a vararg of [Results][Result] all the [Ok] elements. All the [Ok] elements are
|
|
|
|
* extracted in order.
|
|
|
|
*
|
2017-10-21 02:51:30 +00:00
|
|
|
* - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts)
|
2017-10-21 20:19:21 +00:00
|
|
|
*
|
|
|
|
* @param results The [Results][Result] from which to extract [Ok] elements.
|
|
|
|
* @return The extracted [Ok] elements.
|
2017-10-21 02:51:30 +00:00
|
|
|
*/
|
|
|
|
fun <V, E> getAll(vararg results: Result<V, E>) = results.asIterable().getAll()
|
|
|
|
|
|
|
|
/**
|
2017-10-21 20:19:21 +00:00
|
|
|
* Extracts from an [Iterable] of [Results][Result] all the [Ok] elements. All the [Ok] elements
|
|
|
|
* are extracted in order.
|
|
|
|
*
|
2017-10-21 02:51:30 +00:00
|
|
|
* - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts)
|
2017-10-21 20:19:21 +00:00
|
|
|
*
|
|
|
|
* @return The extracted [Ok] elements.
|
2017-10-21 02:51:30 +00:00
|
|
|
*/
|
|
|
|
fun <V, E> Iterable<Result<V, E>>.getAll(): List<V> {
|
2017-10-22 14:24:37 +00:00
|
|
|
return filterIsInstance<Ok<V>>().map { it.value }
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-21 20:19:21 +00:00
|
|
|
* Extracts from a vararg of [Results][Result] all the [Error] elements. All the [Error] elements
|
|
|
|
* are extracted in order.
|
|
|
|
*
|
2017-10-21 02:51:30 +00:00
|
|
|
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
|
2017-10-21 20:19:21 +00:00
|
|
|
*
|
|
|
|
* @param results The [Results][Result] from which to extract [Error] elements.
|
|
|
|
* @return The extracted [Error] elements.
|
2017-10-21 02:51:30 +00:00
|
|
|
*/
|
|
|
|
fun <V, E> getAllErrors(vararg results: Result<V, E>) = results.asIterable().getAllErrors()
|
|
|
|
|
|
|
|
/**
|
2017-10-21 20:19:21 +00:00
|
|
|
* Extracts from an [Iterable] of [Results][Result] all the [Error] elements. All the [Error]
|
|
|
|
* elements are extracted in order.
|
|
|
|
*
|
2017-10-21 02:51:30 +00:00
|
|
|
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
|
2017-10-21 20:19:21 +00:00
|
|
|
*
|
|
|
|
* @return The extracted [Error] elements.
|
2017-10-21 02:51:30 +00:00
|
|
|
*/
|
|
|
|
fun <V, E> Iterable<Result<V, E>>.getAllErrors(): List<E> {
|
2017-10-22 14:24:37 +00:00
|
|
|
return filterIsInstance<Error<E>>().map { it.error }
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-21 20:19:21 +00:00
|
|
|
* Partitions a vararg of [Results][Result] into a [Pair] of [Lists][List]. All the [Ok] elements
|
|
|
|
* are extracted, in order, to the [first][Pair.first] value. Similarly the [Error] elements are
|
|
|
|
* extracted to the [Pair.second] value.
|
|
|
|
*
|
2017-10-21 02:51:30 +00:00
|
|
|
* - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers)
|
2017-10-21 20:19:21 +00:00
|
|
|
*
|
|
|
|
* @param results The [Results][Result] to partition.
|
|
|
|
* @return A [Pair] of [Lists][List] where the [first][Pair.first] value
|
|
|
|
* contains the [Ok] elements and the [second][Pair.second] value contains the [Error] elements.
|
2017-10-21 02:51:30 +00:00
|
|
|
*/
|
|
|
|
fun <V, E> partition(vararg results: Result<V, E>) = results.asIterable().partition()
|
|
|
|
|
|
|
|
/**
|
2017-10-21 20:19:21 +00:00
|
|
|
* Partitions an [Iterable] of [Results][Result] into a [Pair] of [Lists][List]. All the [Ok]
|
|
|
|
* elements are extracted, in order, to the [first][Pair.first] value. Similarly the [Error]
|
|
|
|
* elements are extracted to the [Pair.second] value.
|
|
|
|
*
|
2017-10-21 02:51:30 +00:00
|
|
|
* - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers)
|
2017-10-21 20:19:21 +00:00
|
|
|
*
|
|
|
|
* @return A [Pair] of [Lists][List] where the [first][Pair.first] value
|
|
|
|
* contains the [Ok] elements and the [second][Pair.second] value contains the [Error] elements.
|
2017-10-21 02:51:30 +00:00
|
|
|
*/
|
|
|
|
fun <V, E> Iterable<Result<V, E>>.partition(): Pair<List<V>, List<E>> {
|
|
|
|
val values = mutableListOf<V>()
|
|
|
|
val errors = mutableListOf<E>()
|
|
|
|
|
|
|
|
forEach { result ->
|
|
|
|
when (result) {
|
|
|
|
is Ok -> values.add(result.value)
|
|
|
|
is Error -> errors.add(result.error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pair(values, errors)
|
|
|
|
}
|