From e43a7008d85284d2ce0342d9ed9b3681598b1a0a Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Tue, 18 Sep 2018 19:40:07 +0100 Subject: [PATCH] Add more mapping functions for Iterables - mapAll - mapResult - mapResultTo - mapResultNotNull - mapResultNotNullTo - mapResultIndexed - mapResultIndexedTo - mapResultIndexedNotNull - mapResultIndexedNotNullTo --- .../com/github/michaelbull/result/Iterable.kt | 81 +++++++++++++++++++ .../com/github/michaelbull/result/Map.kt | 12 +++ 2 files changed, 93 insertions(+) diff --git a/src/main/kotlin/com/github/michaelbull/result/Iterable.kt b/src/main/kotlin/com/github/michaelbull/result/Iterable.kt index 1507bb1..ccaf71f 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Iterable.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Iterable.kt @@ -135,3 +135,84 @@ fun Iterable>.partition(): Pair, List> { return Pair(values, errors) } + +/** + * Returns a [Result, E>][Result] containing the results of applying the given [transform] function to each + * element in the original collection, returning early with the first [Err] if a transformation fails. + */ +fun Iterable.mapResult(transform: (V) -> Result): Result, E> { + return Ok(map { element -> + transform(element).getOrElse { return Err(it) } + }) +} + +/** + * Applies the given [transform] function to each element of the original collection and appends the results to the + * given [destination], returning early with the first [Err] if a transformation fails. + */ +fun > Iterable.mapResultTo(destination: C, transform: (V) -> Result): Result { + return Ok(mapTo(destination) { element -> + transform(element).getOrElse { return Err(it) } + }) +} + +/** + * Returns a [Result, E>][Result] containing only the non-null results of applying the given [transform] + * function to each element in the original collection, returning early with the first [Err] if a transformation fails. + */ +fun Iterable.mapResultNotNull(transform: (V) -> Result?): Result, E> { + return Ok(mapNotNull { element -> + transform(element)?.getOrElse { return Err(it) } + }) +} + +/** + * Applies the given [transform] function to each element in the original collection and appends only the non-null + * results to the given [destination], returning early with the first [Err] if a transformation fails. + */ +fun > Iterable.mapResultNotNullTo(destination: C, transform: (V) -> Result?): Result { + return Ok(mapNotNullTo(destination) { element -> + transform(element)?.getOrElse { return Err(it) } + }) +} + +/** + * Returns a [Result, E>][Result] containing the results of applying the given [transform] function to each + * element and its index in the original collection, returning early with the first [Err] if a transformation fails. + */ +fun Iterable.mapResultIndexed(transform: (index: Int, V) -> Result): Result, E> { + return Ok(mapIndexed { index, element -> + transform(index, element).getOrElse { return Err(it) } + }) +} + +/** + * Applies the given [transform] function to each element and its index in the original collection and appends the + * results to the given [destination], returning early with the first [Err] if a transformation fails. + */ +fun > Iterable.mapResultIndexedTo(destination: C, transform: (index: Int, V) -> Result): Result { + return Ok(mapIndexedTo(destination) { index, element -> + transform(index, element).getOrElse { return Err(it) } + }) +} + +/** + * Returns a [Result, E>][Result] containing only the non-null results of applying the given [transform] + * function to each element and its index in the original collection, returning early with the first [Err] if a + * transformation fails. + */ +fun Iterable.mapResultIndexedNotNull(transform: (index: Int, V) -> Result?): Result, E> { + return Ok(mapIndexedNotNull { index, element -> + transform(index, element)?.getOrElse { return Err(it) } + }) +} + +/** + * Applies the given [transform] function to each element and its index in the original collection and appends only the + * non-null results to the given [destination], returning early with the first [Err] if a transformation fails. + */ +fun > Iterable.mapResultIndexedNotNullTo(destination: C, transform: (index: Int, V) -> Result?): Result { + return Ok(mapIndexedNotNullTo(destination) { index, element -> + transform(index, element)?.getOrElse { return Err(it) } + }) +} diff --git a/src/main/kotlin/com/github/michaelbull/result/Map.kt b/src/main/kotlin/com/github/michaelbull/result/Map.kt index 46e3fb1..015fba3 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Map.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Map.kt @@ -30,6 +30,18 @@ inline infix fun Result.mapError(transform: (E) -> F): Result, E>][Result] containing the results of applying the given [transform] function to each + * element in the original collection, returning early with the first [Err] if a transformation fails. + */ +fun Result, E>.mapAll(transform: (V) -> Result): Result, E> { + return map { iterable -> + iterable.map { element -> + transform(element).getOrElse { return Err(it) } + } + } +} + /** * Maps this [Result][Result] to `U` by applying either the [success] function if this [Result] * is [Ok], or the [failure] function if this [Result] is an [Err]. Both of these functions must