From 9efdd1ca9c383a94ff82585f8c6a67d8e9de75a9 Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Fri, 8 Mar 2024 14:29:56 +0000 Subject: [PATCH] Reformat Iterable --- .../com/github/michaelbull/result/Iterable.kt | 73 +++++++++++++------ 1 file changed, 49 insertions(+), 24 deletions(-) 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 051e40a..4e14095 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 @@ -4,7 +4,10 @@ package com.github.michaelbull.result * Accumulates value starting with [initial] value and applying [operation] from left to right to * current accumulator value and each element. */ -public inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> Result): Result { +public inline fun Iterable.fold( + initial: R, + operation: (acc: R, T) -> Result, +): Result { var accumulator = initial for (element in this) { @@ -21,11 +24,15 @@ public inline fun Iterable.fold(initial: R, operation: (acc: R, T) * Accumulates value starting with [initial] value and applying [operation] from right to left to * each element and current accumulator value. */ -public inline fun List.foldRight(initial: R, operation: (T, acc: R) -> Result): Result { +public inline fun > List.foldRight( + initial: R, + operation: (T, acc: R) -> Result, +): Result { var accumulator = initial if (!isEmpty()) { val iterator = listIterator(size) + while (iterator.hasPrevious()) { accumulator = when (val result = operation(iterator.previous(), accumulator)) { is Ok -> result.value @@ -43,7 +50,7 @@ public inline fun List.foldRight(initial: R, operation: (T, acc: R) * * - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#combine) */ -public fun combine(vararg results: Result): Result, E> { +public fun > combine(vararg results: R): Result, E> { return results.asIterable().combine() } @@ -54,12 +61,14 @@ public fun combine(vararg results: Result): Result, E> { * - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#combine) */ public fun Iterable>.combine(): Result, E> { - return Ok(map { + val values = map { when (it) { is Ok -> it.value is Err -> return it } - }) + } + + return Ok(values) } /** @@ -68,7 +77,7 @@ public fun Iterable>.combine(): Result, E> { * * - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts) */ -public fun getAll(vararg results: Result): List { +public fun > getAll(vararg results: R): List { return results.asIterable().getAll() } @@ -88,7 +97,7 @@ public fun Iterable>.getAll(): List { * * - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights) */ -public fun getAllErrors(vararg results: Result): List = results.asIterable().getAllErrors() +public fun > getAllErrors(vararg results: R): List = results.asIterable().getAllErrors() /** * Extracts from an [Iterable] of [Results][Result] all the [Err] elements. All the [Err] elements @@ -107,7 +116,7 @@ public fun Iterable>.getAllErrors(): List { * * - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers) */ -public fun partition(vararg results: Result): Pair, List> { +public fun > partition(vararg results: R): Pair, List> { return results.asIterable().partition() } @@ -140,12 +149,14 @@ public fun Iterable>.partition(): Pair, List> { public inline fun Iterable.mapResult( transform: (V) -> Result, ): Result, E> { - return Ok(map { element -> + val values = map { element -> when (val transformed = transform(element)) { is Ok -> transformed.value is Err -> return transformed } - }) + } + + return Ok(values) } /** @@ -157,12 +168,14 @@ public inline fun > Iterable.mapResultTo destination: C, transform: (V) -> Result, ): Result { - return Ok(mapTo(destination) { element -> + val values = mapTo(destination) { element -> when (val transformed = transform(element)) { is Ok -> transformed.value is Err -> return transformed } - }) + } + + return Ok(values) } /** @@ -174,13 +187,15 @@ public inline fun > Iterable.mapResultTo public inline fun Iterable.mapResultNotNull( transform: (V) -> Result?, ): Result, E> { - return Ok(mapNotNull { element -> + val values = mapNotNull { element -> when (val transformed = transform(element)) { is Ok -> transformed.value is Err -> return transformed null -> null } - }) + } + + return Ok(values) } /** @@ -192,13 +207,15 @@ public inline fun > Iterable.mapRe destination: C, transform: (V) -> Result?, ): Result { - return Ok(mapNotNullTo(destination) { element -> + val values = mapNotNullTo(destination) { element -> when (val transformed = transform(element)) { is Ok -> transformed.value is Err -> return transformed null -> null } - }) + } + + return Ok(values) } /** @@ -210,12 +227,14 @@ public inline fun > Iterable.mapRe public inline fun Iterable.mapResultIndexed( transform: (index: Int, V) -> Result, ): Result, E> { - return Ok(mapIndexed { index, element -> + val values = mapIndexed { index, element -> when (val transformed = transform(index, element)) { is Ok -> transformed.value is Err -> return transformed } - }) + } + + return Ok(values) } /** @@ -227,12 +246,14 @@ public inline fun > Iterable.mapResultIn destination: C, transform: (index: Int, V) -> Result, ): Result { - return Ok(mapIndexedTo(destination) { index, element -> + val values = mapIndexedTo(destination) { index, element -> when (val transformed = transform(index, element)) { is Ok -> transformed.value is Err -> return transformed } - }) + } + + return Ok(values) } /** @@ -244,13 +265,15 @@ public inline fun > Iterable.mapResultIn public inline fun Iterable.mapResultIndexedNotNull( transform: (index: Int, V) -> Result?, ): Result, E> { - return Ok(mapIndexedNotNull { index, element -> + val values = mapIndexedNotNull { index, element -> when (val transformed = transform(index, element)) { is Ok -> transformed.value is Err -> return transformed null -> null } - }) + } + + return Ok(values) } /** @@ -262,11 +285,13 @@ public inline fun > Iterable.mapRe destination: C, transform: (index: Int, V) -> Result?, ): Result { - return Ok(mapIndexedNotNullTo(destination) { index, element -> + val values = mapIndexedNotNullTo(destination) { index, element -> when (val transformed = transform(index, element)) { is Ok -> transformed.value is Err -> return transformed null -> null } - }) + } + + return Ok(values) }