Skip to content

Commit

Permalink
Add Iterable#{allOk,allErr,anyOk,anyErr,countOk,countErr}
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Mar 8, 2024
1 parent 15fc1ff commit 6e62d9f
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
package com.github.michaelbull.result

/**
* Returns `true` if each element is [Ok], `false` otherwise.
*/
public fun <V, E> Iterable<Result<V, E>>.allOk(): Boolean {
return all { it is Ok }
}

/**
* Returns `true` if each element is [Err], `false` otherwise.
*/
public fun <V, E> Iterable<Result<V, E>>.allErr(): Boolean {
return all { it is Err }
}

/**
* Returns `true` if at least one element is [Ok], `false` otherwise.
*/
public fun <V, E> Iterable<Result<V, E>>.anyOk(): Boolean {
return any { it is Ok }
}

/**
* Returns `true` if at least one element is [Err], `false` otherwise.
*/
public fun <V, E> Iterable<Result<V, E>>.anyErr(): Boolean {
return any { it is Err }
}

/**
* Returns the number of elements that are [Ok].
*/
public fun <V, E> Iterable<Result<V, E>>.countOk(): Int {
return count { it is Ok }
}

/**
* Returns the number of elements that are [Err].
*/
public fun <V, E> Iterable<Result<V, E>>.countErr(): Int {
return count { it is Err }
}

/**
* Accumulates value starting with [initial] value and applying [operation] from left to right to
* current accumulator value and each element.
Expand Down

0 comments on commit 6e62d9f

Please sign in to comment.