Inline "when" variable declarations
This commit is contained in:
parent
1ab20a0c76
commit
65b9065164
@ -7,14 +7,10 @@ package com.github.michaelbull.result
|
||||
inline fun <T, R, E> Iterable<T>.fold(initial: R, operation: (acc: R, T) -> Result<R, E>): Result<R, E> {
|
||||
var accumulator = initial
|
||||
|
||||
forEach { element ->
|
||||
val operationResult = operation(accumulator, element)
|
||||
|
||||
when (operationResult) {
|
||||
is Ok -> {
|
||||
accumulator = operationResult.value
|
||||
}
|
||||
is Err -> return Err(operationResult.error)
|
||||
for (element in this) {
|
||||
accumulator = when (val result = operation(accumulator, element)) {
|
||||
is Ok -> result.value
|
||||
is Err -> return Err(result.error)
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,13 +27,9 @@ inline fun <T, R, E> List<T>.foldRight(initial: R, operation: (T, acc: R) -> Res
|
||||
if (!isEmpty()) {
|
||||
val iterator = listIterator(size)
|
||||
while (iterator.hasPrevious()) {
|
||||
val operationResult = operation(iterator.previous(), accumulator)
|
||||
|
||||
when (operationResult) {
|
||||
is Ok -> {
|
||||
accumulator = operationResult.value
|
||||
}
|
||||
is Err -> return Err(operationResult.error)
|
||||
accumulator = when (val result = operation(iterator.previous(), accumulator)) {
|
||||
is Ok -> result.value
|
||||
is Err -> return Err(result.error)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -143,11 +135,11 @@ fun <V, E> Iterable<Result<V, E>>.partition(): Pair<List<V>, List<E>> {
|
||||
* function to each element in the original collection, returning early with the first [Err] if a
|
||||
* transformation fails.
|
||||
*/
|
||||
inline fun <V, E, U> Iterable<V>.mapResult(transform: (V) -> Result<U, E>): Result<List<U>, E> {
|
||||
inline fun <V, E, U> Iterable<V>.mapResult(
|
||||
transform: (V) -> Result<U, E>
|
||||
): Result<List<U>, E> {
|
||||
return Ok(map { element ->
|
||||
val transformed = transform(element)
|
||||
|
||||
when (transformed) {
|
||||
when (val transformed = transform(element)) {
|
||||
is Ok -> transformed.value
|
||||
is Err -> return transformed
|
||||
}
|
||||
@ -159,11 +151,12 @@ inline fun <V, E, U> Iterable<V>.mapResult(transform: (V) -> Result<U, E>): Resu
|
||||
* the results to the given [destination], returning early with the first [Err] if a
|
||||
* transformation fails.
|
||||
*/
|
||||
inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo(destination: C, transform: (V) -> Result<U, E>): Result<C, E> {
|
||||
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 transformed = transform(element)
|
||||
|
||||
when (transformed) {
|
||||
when (val transformed = transform(element)) {
|
||||
is Ok -> transformed.value
|
||||
is Err -> return transformed
|
||||
}
|
||||
@ -175,11 +168,11 @@ inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo(destin
|
||||
* given [transform] function to each element in the original collection, returning early with the
|
||||
* first [Err] if a transformation fails.
|
||||
*/
|
||||
inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull(transform: (V) -> Result<U, E>?): Result<List<U>, E> {
|
||||
inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull(
|
||||
transform: (V) -> Result<U, E>?
|
||||
): Result<List<U>, E> {
|
||||
return Ok(mapNotNull { element ->
|
||||
val transformed = transform(element)
|
||||
|
||||
when (transformed) {
|
||||
when (val transformed = transform(element)) {
|
||||
is Ok -> transformed.value
|
||||
is Err -> return transformed
|
||||
null -> null
|
||||
@ -192,11 +185,12 @@ inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull(transform: (V) -> Result
|
||||
* only the non-null results to the given [destination], returning early with the first [Err] if a
|
||||
* transformation fails.
|
||||
*/
|
||||
inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultNotNullTo(destination: C, transform: (V) -> Result<U, E>?): Result<C, E> {
|
||||
inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultNotNullTo(
|
||||
destination: C,
|
||||
transform: (V) -> Result<U, E>?
|
||||
): Result<C, E> {
|
||||
return Ok(mapNotNullTo(destination) { element ->
|
||||
val transformed = transform(element)
|
||||
|
||||
when (transformed) {
|
||||
when (val transformed = transform(element)) {
|
||||
is Ok -> transformed.value
|
||||
is Err -> return transformed
|
||||
null -> null
|
||||
@ -209,11 +203,11 @@ inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultNot
|
||||
* function to each element and its index in the original collection, returning early with the
|
||||
* first [Err] if a transformation fails.
|
||||
*/
|
||||
inline fun <V, E, U> Iterable<V>.mapResultIndexed(transform: (index: Int, V) -> Result<U, E>): Result<List<U>, E> {
|
||||
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 transformed = transform(index, element)
|
||||
|
||||
when (transformed) {
|
||||
when (val transformed = transform(index, element)) {
|
||||
is Ok -> transformed.value
|
||||
is Err -> return transformed
|
||||
}
|
||||
@ -225,11 +219,12 @@ inline fun <V, E, U> Iterable<V>.mapResultIndexed(transform: (index: Int, V) ->
|
||||
* and appends the results to the given [destination], returning early with the first [Err] if a
|
||||
* transformation fails.
|
||||
*/
|
||||
inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedTo(destination: C, transform: (index: Int, V) -> Result<U, E>): Result<C, E> {
|
||||
inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedTo(
|
||||
destination: C,
|
||||
transform: (index: Int, V) -> Result<U, E>
|
||||
): Result<C, E> {
|
||||
return Ok(mapIndexedTo(destination) { index, element ->
|
||||
val transformed = transform(index, element)
|
||||
|
||||
when (transformed) {
|
||||
when (val transformed = transform(index, element)) {
|
||||
is Ok -> transformed.value
|
||||
is Err -> return transformed
|
||||
}
|
||||
@ -241,11 +236,11 @@ inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedTo
|
||||
* given [transform] function to each element and its index in the original collection, returning
|
||||
* early with the first [Err] if a transformation fails.
|
||||
*/
|
||||
inline fun <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull(transform: (index: Int, V) -> Result<U, E>?): Result<List<U>, E> {
|
||||
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 transformed = transform(index, element)
|
||||
|
||||
when (transformed) {
|
||||
when (val transformed = transform(index, element)) {
|
||||
is Ok -> transformed.value
|
||||
is Err -> return transformed
|
||||
null -> null
|
||||
@ -258,11 +253,12 @@ inline fun <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull(transform: (index
|
||||
* and appends only the non-null results to the given [destination], returning early with the first
|
||||
* [Err] if a transformation fails.
|
||||
*/
|
||||
inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedNotNullTo(destination: C, transform: (index: Int, V) -> Result<U, E>?): Result<C, E> {
|
||||
inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedNotNullTo(
|
||||
destination: C,
|
||||
transform: (index: Int, V) -> Result<U, E>?
|
||||
): Result<C, E> {
|
||||
return Ok(mapIndexedNotNullTo(destination) { index, element ->
|
||||
val transformed = transform(index, element)
|
||||
|
||||
when (transformed) {
|
||||
when (val transformed = transform(index, element)) {
|
||||
is Ok -> transformed.value
|
||||
is Err -> return transformed
|
||||
null -> null
|
||||
|
@ -38,9 +38,7 @@ inline infix fun <V, E, F> Result<V, E>.mapError(transform: (E) -> F): Result<V,
|
||||
inline infix fun <V, E, U> Result<Iterable<V>, E>.mapAll(transform: (V) -> Result<U, E>): Result<List<U>, E> {
|
||||
return map { iterable ->
|
||||
iterable.map { element ->
|
||||
val transformed = transform(element)
|
||||
|
||||
when (transformed) {
|
||||
when (val transformed = transform(element)) {
|
||||
is Ok -> transformed.value
|
||||
is Err -> return transformed
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user