Inline "when" variable declarations

This commit is contained in:
Michael Bull 2019-05-30 11:48:12 +01:00
parent 1ab20a0c76
commit 65b9065164
2 changed files with 44 additions and 50 deletions

View File

@ -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> { 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
forEach { element -> for (element in this) {
val operationResult = operation(accumulator, element) accumulator = when (val result = operation(accumulator, element)) {
is Ok -> result.value
when (operationResult) { is Err -> return Err(result.error)
is Ok -> {
accumulator = operationResult.value
}
is Err -> return Err(operationResult.error)
} }
} }
@ -31,13 +27,9 @@ inline fun <T, R, E> List<T>.foldRight(initial: R, operation: (T, acc: R) -> Res
if (!isEmpty()) { if (!isEmpty()) {
val iterator = listIterator(size) val iterator = listIterator(size)
while (iterator.hasPrevious()) { while (iterator.hasPrevious()) {
val operationResult = operation(iterator.previous(), accumulator) accumulator = when (val result = operation(iterator.previous(), accumulator)) {
is Ok -> result.value
when (operationResult) { is Err -> return Err(result.error)
is Ok -> {
accumulator = operationResult.value
}
is Err -> return Err(operationResult.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 * function to each element in the original collection, returning early with the first [Err] if a
* transformation fails. * 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 -> return Ok(map { element ->
val transformed = transform(element) when (val transformed = transform(element)) {
when (transformed) {
is Ok -> transformed.value is Ok -> transformed.value
is Err -> return transformed 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 * the results to the given [destination], returning early with the first [Err] if a
* transformation fails. * 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 -> return Ok(mapTo(destination) { element ->
val transformed = transform(element) when (val transformed = transform(element)) {
when (transformed) {
is Ok -> transformed.value is Ok -> transformed.value
is Err -> return transformed 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 * given [transform] function to each element in the original collection, returning early with the
* first [Err] if a transformation fails. * 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 -> return Ok(mapNotNull { element ->
val transformed = transform(element) when (val transformed = transform(element)) {
when (transformed) {
is Ok -> transformed.value is Ok -> transformed.value
is Err -> return transformed is Err -> return transformed
null -> null 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 * only the non-null results to the given [destination], returning early with the first [Err] if a
* transformation fails. * 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 -> return Ok(mapNotNullTo(destination) { element ->
val transformed = transform(element) when (val transformed = transform(element)) {
when (transformed) {
is Ok -> transformed.value is Ok -> transformed.value
is Err -> return transformed is Err -> return transformed
null -> null 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 * function to each element and its index in the original collection, returning early with the
* first [Err] if a transformation fails. * 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 -> return Ok(mapIndexed { index, element ->
val transformed = transform(index, element) when (val transformed = transform(index, element)) {
when (transformed) {
is Ok -> transformed.value is Ok -> transformed.value
is Err -> return transformed 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 * and appends the results to the given [destination], returning early with the first [Err] if a
* transformation fails. * 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 -> return Ok(mapIndexedTo(destination) { index, element ->
val transformed = transform(index, element) when (val transformed = transform(index, element)) {
when (transformed) {
is Ok -> transformed.value is Ok -> transformed.value
is Err -> return transformed 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 * given [transform] function to each element and its index in the original collection, returning
* early with the first [Err] if a transformation fails. * 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 -> return Ok(mapIndexedNotNull { index, element ->
val transformed = transform(index, element) when (val transformed = transform(index, element)) {
when (transformed) {
is Ok -> transformed.value is Ok -> transformed.value
is Err -> return transformed is Err -> return transformed
null -> null 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 * and appends only the non-null results to the given [destination], returning early with the first
* [Err] if a transformation fails. * [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 -> return Ok(mapIndexedNotNullTo(destination) { index, element ->
val transformed = transform(index, element) when (val transformed = transform(index, element)) {
when (transformed) {
is Ok -> transformed.value is Ok -> transformed.value
is Err -> return transformed is Err -> return transformed
null -> null null -> null

View File

@ -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> { inline infix fun <V, E, U> Result<Iterable<V>, E>.mapAll(transform: (V) -> Result<U, E>): Result<List<U>, E> {
return map { iterable -> return map { iterable ->
iterable.map { element -> iterable.map { element ->
val transformed = transform(element) when (val transformed = transform(element)) {
when (transformed) {
is Ok -> transformed.value is Ok -> transformed.value
is Err -> return transformed is Err -> return transformed
} }