From 65b9065164f006eccd6c5d35bcbce87c49c27445 Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Thu, 30 May 2019 11:48:12 +0100 Subject: [PATCH] Inline "when" variable declarations --- .../com/github/michaelbull/result/Iterable.kt | 90 +++++++++---------- .../com/github/michaelbull/result/Map.kt | 4 +- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/src/main/kotlin/com/github/michaelbull/result/Iterable.kt b/src/main/kotlin/com/github/michaelbull/result/Iterable.kt index 2b7a149..3cd75b1 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Iterable.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Iterable.kt @@ -7,14 +7,10 @@ package com.github.michaelbull.result inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> Result): Result { 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 List.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 Iterable>.partition(): Pair, List> { * function to each element in the original collection, returning early with the first [Err] if a * transformation fails. */ -inline fun Iterable.mapResult(transform: (V) -> Result): Result, E> { +inline fun Iterable.mapResult( + transform: (V) -> Result +): Result, 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 Iterable.mapResult(transform: (V) -> Result): Resu * the results to the given [destination], returning early with the first [Err] if a * transformation fails. */ -inline fun > Iterable.mapResultTo(destination: C, transform: (V) -> Result): Result { +inline fun > Iterable.mapResultTo( + destination: C, + transform: (V) -> Result +): Result { 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 > Iterable.mapResultTo(destin * given [transform] function to each element in the original collection, returning early with the * first [Err] if a transformation fails. */ -inline fun Iterable.mapResultNotNull(transform: (V) -> Result?): Result, E> { +inline fun Iterable.mapResultNotNull( + transform: (V) -> Result? +): Result, 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 Iterable.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 > Iterable.mapResultNotNullTo(destination: C, transform: (V) -> Result?): Result { +inline fun > Iterable.mapResultNotNullTo( + destination: C, + transform: (V) -> Result? +): Result { 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 > Iterable.mapResultNot * function to each element and its index in the original collection, returning early with the * first [Err] if a transformation fails. */ -inline fun Iterable.mapResultIndexed(transform: (index: Int, V) -> Result): Result, E> { +inline fun Iterable.mapResultIndexed( + transform: (index: Int, V) -> Result +): Result, 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 Iterable.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 > Iterable.mapResultIndexedTo(destination: C, transform: (index: Int, V) -> Result): Result { +inline fun > Iterable.mapResultIndexedTo( + destination: C, + transform: (index: Int, V) -> Result +): Result { 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 > Iterable.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 Iterable.mapResultIndexedNotNull(transform: (index: Int, V) -> Result?): Result, E> { +inline fun Iterable.mapResultIndexedNotNull( + transform: (index: Int, V) -> Result? +): Result, 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 Iterable.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 > Iterable.mapResultIndexedNotNullTo(destination: C, transform: (index: Int, V) -> Result?): Result { +inline fun > Iterable.mapResultIndexedNotNullTo( + destination: C, + transform: (index: Int, V) -> Result? +): Result { 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 diff --git a/src/main/kotlin/com/github/michaelbull/result/Map.kt b/src/main/kotlin/com/github/michaelbull/result/Map.kt index 7c2d579..32800b7 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Map.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Map.kt @@ -38,9 +38,7 @@ inline infix fun Result.mapError(transform: (E) -> F): Result Result, E>.mapAll(transform: (V) -> Result): Result, 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 }