Skip to content

Commit

Permalink
Reformat Iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Mar 8, 2024
1 parent 683b1b7 commit 9efdd1c
Showing 1 changed file with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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

for (element in this) {
Expand All @@ -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
* 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

if (!isEmpty()) {
val iterator = listIterator(size)

while (iterator.hasPrevious()) {
accumulator = when (val result = operation(iterator.previous(), accumulator)) {
is Ok -> result.value
Expand All @@ -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)
*/
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()
}

Expand All @@ -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)
*/
public fun <V, E> Iterable<Result<V, E>>.combine(): Result<List<V>, E> {
return Ok(map {
val values = map {
when (it) {
is Ok -> it.value
is Err -> return it
}
})
}

return Ok(values)
}

/**
Expand All @@ -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)
*/
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()
}

Expand All @@ -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)
*/
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
Expand All @@ -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)
*/
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()
}

Expand Down Expand Up @@ -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(
transform: (V) -> Result<U, E>,
): Result<List<U>, 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)
}

/**
Expand All @@ -157,12 +168,14 @@ public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo
destination: C,
transform: (V) -> Result<U, E>,
): Result<C, E> {
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)
}

/**
Expand All @@ -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(
transform: (V) -> Result<U, E>?,
): Result<List<U>, 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)
}

/**
Expand All @@ -192,13 +207,15 @@ public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapRe
destination: C,
transform: (V) -> Result<U, E>?,
): Result<C, E> {
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)
}

/**
Expand All @@ -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(
transform: (index: Int, V) -> Result<U, E>,
): Result<List<U>, 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)
}

/**
Expand All @@ -227,12 +246,14 @@ public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIn
destination: C,
transform: (index: Int, V) -> Result<U, E>,
): Result<C, E> {
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)
}

/**
Expand All @@ -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(
transform: (index: Int, V) -> Result<U, E>?,
): Result<List<U>, 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)
}

/**
Expand All @@ -262,11 +285,13 @@ public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapRe
destination: C,
transform: (index: Int, V) -> Result<U, E>?,
): Result<C, E> {
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)
}

0 comments on commit 9efdd1c

Please sign in to comment.