Skip to content

Commit

Permalink
Add more mapping functions for Iterables
Browse files Browse the repository at this point in the history
- mapAll
- mapResult
- mapResultTo
- mapResultNotNull
- mapResultNotNullTo
- mapResultIndexed
- mapResultIndexedTo
- mapResultIndexedNotNull
- mapResultIndexedNotNullTo
  • Loading branch information
michaelbull committed Sep 18, 2018
1 parent cc1ab49 commit e43a700
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/main/kotlin/com/github/michaelbull/result/Iterable.kt
Expand Up @@ -135,3 +135,84 @@ fun <V, E> Iterable<Result<V, E>>.partition(): Pair<List<V>, List<E>> {

return Pair(values, errors)
}

/**
* Returns a [Result<List<U>, 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 <V, E, U> Iterable<V>.mapResult(transform: (V) -> Result<U, E>): Result<List<U>, 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 <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 ->
transform(element).getOrElse { return Err(it) }
})
}

/**
* Returns a [Result<List<U>, 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 <V, E, U : Any> Iterable<V>.mapResultNotNull(transform: (V) -> Result<U, E>?): Result<List<U>, 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 <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 ->
transform(element)?.getOrElse { return Err(it) }
})
}

/**
* Returns a [Result<List<U>, 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 <V, E, U> Iterable<V>.mapResultIndexed(transform: (index: Int, V) -> Result<U, E>): Result<List<U>, 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 <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 ->
transform(index, element).getOrElse { return Err(it) }
})
}

/**
* Returns a [Result<List<U>, 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 <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull(transform: (index: Int, V) -> Result<U, E>?): Result<List<U>, 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 <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 ->
transform(index, element)?.getOrElse { return Err(it) }
})
}
12 changes: 12 additions & 0 deletions src/main/kotlin/com/github/michaelbull/result/Map.kt
Expand Up @@ -30,6 +30,18 @@ inline infix fun <V, E, F> Result<V, E>.mapError(transform: (E) -> F): Result<V,
}
}

/**
* Returns a [Result<List<U>, 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 <V, E, U> Result<Iterable<V>, E>.mapAll(transform: (V) -> Result<U, E>): Result<List<U>, E> {
return map { iterable ->
iterable.map { element ->
transform(element).getOrElse { return Err(it) }
}
}
}

/**
* Maps this [Result<V, E>][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
Expand Down

0 comments on commit e43a700

Please sign in to comment.