Reformat Iterable

This commit is contained in:
Michael Bull 2024-03-08 14:29:56 +00:00
parent 01c8c00f98
commit d2932de547
1 changed files with 49 additions and 24 deletions

View File

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