Skip to content

Commit

Permalink
Add Iterable#{filterValues,filterValuesTo,filterErrors,filterErrorsTo}
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Mar 8, 2024
1 parent 6e62d9f commit f091f50
Showing 1 changed file with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package com.github.michaelbull.result

/**
* Returns a list containing only elements that are [Ok].
*/
public fun <V, E> Iterable<Result<V, E>>.filterValues(): List<V> {
return filterValuesTo(ArrayList())
}

/**
* Returns a list containing only elements that are [Err].
*/
public fun <V, E> Iterable<Result<V, E>>.filterErrors(): List<E> {
return filterErrorsTo(ArrayList())
}

/**
* Appends the [values][Ok.value] of each element that is [Ok] to the given [destination].
*/
public fun <V, E, C : MutableCollection<in V>> Iterable<Result<V, E>>.filterValuesTo(destination: C): C {
for (element in this) {
if (element is Ok<V>) {
destination.add(element.value)
}
}

return destination
}

/**
* Appends the [errors][Err.error] of each element that is [Err] to the given [destination].
*/
public fun <V, E, C : MutableCollection<in E>> Iterable<Result<V, E>>.filterErrorsTo(destination: C): C {
for (element in this) {
if (element is Err<E>) {
destination.add(element.error)
}
}

return destination
}

/**
* Returns `true` if each element is [Ok], `false` otherwise.
*/
Expand Down Expand Up @@ -120,7 +160,7 @@ public fun <V, E> Iterable<Result<V, E>>.combine(): Result<List<V>, E> {
* - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts)
*/
public fun <V, E, R : Result<V, E>> getAll(vararg results: R): List<V> {
return results.asIterable().getAll()
return results.asIterable().filterValues()
}

/**
Expand All @@ -139,7 +179,9 @@ public fun <V, E> Iterable<Result<V, E>>.getAll(): List<V> {
*
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
*/
public fun <V, E, R : Result<V, E>> getAllErrors(vararg results: R): List<E> = results.asIterable().getAllErrors()
public fun <V, E, R : Result<V, E>> getAllErrors(vararg results: R): List<E> {
return results.asIterable().filterErrors()
}

/**
* Extracts from an [Iterable] of [Results][Result] all the [Err] elements. All the [Err] elements
Expand Down

0 comments on commit f091f50

Please sign in to comment.