From 6e62d9f97df43c7d2e10fe407ddc110aeb85840d Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Fri, 8 Mar 2024 22:31:53 +0000 Subject: [PATCH] Add Iterable#{allOk,allErr,anyOk,anyErr,countOk,countErr} --- .../com/github/michaelbull/result/Iterable.kt | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt index 4e14095..7fc7801 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt @@ -1,5 +1,47 @@ package com.github.michaelbull.result +/** + * Returns `true` if each element is [Ok], `false` otherwise. + */ +public fun Iterable>.allOk(): Boolean { + return all { it is Ok } +} + +/** + * Returns `true` if each element is [Err], `false` otherwise. + */ +public fun Iterable>.allErr(): Boolean { + return all { it is Err } +} + +/** + * Returns `true` if at least one element is [Ok], `false` otherwise. + */ +public fun Iterable>.anyOk(): Boolean { + return any { it is Ok } +} + +/** + * Returns `true` if at least one element is [Err], `false` otherwise. + */ +public fun Iterable>.anyErr(): Boolean { + return any { it is Err } +} + +/** + * Returns the number of elements that are [Ok]. + */ +public fun Iterable>.countOk(): Int { + return count { it is Ok } +} + +/** + * Returns the number of elements that are [Err]. + */ +public fun Iterable>.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.