Skip to content

Commit

Permalink
Consistently wrap documentation at 100 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Sep 18, 2018
1 parent 29e21e5 commit 1377350
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/github/michaelbull/result/And.kt
Expand Up @@ -18,8 +18,8 @@ inline infix fun <V, E> Result<V, E>.and(result: () -> Result<V, E>): Result<V,
}

/**
* Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform] function
* if this [Result] is [Ok], or returning this [Err].
* Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform]
* function if this [Result] is [Ok], or returning this [Err].
*
* - Elm: [Result.andThen](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#andThen)
* - Rust: [Result.and_then](https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then)
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/github/michaelbull/result/Get.kt
Expand Up @@ -68,8 +68,8 @@ inline infix fun <V, E> Result<V, E>.getErrorOr(default: () -> E): E {
}

/**
* Returns the [value][Ok.value] if this [Result] is [Ok], otherwise
* the [transformation][transform] of the [error][Err.error].
* Returns the [value][Ok.value] if this [Result] is [Ok], otherwise the
* [transformation][transform] of the [error][Err.error].
*
* - Elm: [Result.extract](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#extract)
* - Rust: [Result.unwrap_or_else](https://doc.rust-lang.org/src/core/result.rs.html#735-740)
Expand All @@ -82,8 +82,8 @@ inline infix fun <V, E> Result<V, E>.getOrElse(transform: (E) -> V): V {
}

/**
* Returns the [error][Err.error] if this [Result] is [Err], otherwise
* the [transformation][transform] of the [value][Ok.value].
* Returns the [error][Err.error] if this [Result] is [Err], otherwise the
* [transformation][transform] of the [value][Ok.value].
*/
inline infix fun <V, E> Result<V, E>.getErrorOrElse(transform: (V) -> E): E {
return when (this) {
Expand Down
77 changes: 42 additions & 35 deletions src/main/kotlin/com/github/michaelbull/result/Iterable.kt
@@ -1,12 +1,10 @@
package com.github.michaelbull.result

/**
* Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element.
* Accumulates value starting with [initial] value and applying [operation] from left to right to
* current accumulator value and each element.
*/
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

forEach { element ->
Expand All @@ -24,12 +22,10 @@ inline fun <T, R, E> Iterable<T>.fold(
}

/**
* Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value.
* Accumulates value starting with [initial] value and applying [operation] from right to left to
* each element and current accumulator value.
*/
inline fun <T, R, E> List<T>.foldRight(
initial: R,
operation: (T, acc: R) -> Result<R, E>
): Result<R, E> {
inline fun <T, R, E> List<T>.foldRight(initial: R, operation: (T, acc: R) -> Result<R, E>): Result<R, E> {
var accumulator = initial

if (!isEmpty()) {
Expand Down Expand Up @@ -78,7 +74,9 @@ fun <V, E> Iterable<Result<V, E>>.combine(): Result<List<V>, E> {
*
* - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts)
*/
fun <V, E> getAll(vararg results: Result<V, E>) = results.asIterable().getAll()
fun <V, E> getAll(vararg results: Result<V, E>): List<V> {
return results.asIterable().getAll()
}

/**
* Extracts from an [Iterable] of [Results][Result] all the [Ok] elements. All the [Ok] elements
Expand All @@ -91,16 +89,16 @@ fun <V, E> Iterable<Result<V, E>>.getAll(): List<V> {
}

/**
* Extracts from a vararg of [Results][Result] all the [Err] elements. All the [Err] elements
* are extracted in order.
* Extracts from a vararg of [Results][Result] all the [Err] elements. All the [Err] elements are
* extracted in order.
*
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
*/
fun <V, E> getAllErrors(vararg results: Result<V, E>) = results.asIterable().getAllErrors()

/**
* Extracts from an [Iterable] of [Results][Result] all the [Err] elements. All the [Err]
* elements are extracted in order.
* Extracts from an [Iterable] of [Results][Result] all the [Err] elements. All the [Err] elements
* are extracted in order.
*
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
*/
Expand All @@ -115,12 +113,14 @@ fun <V, E> Iterable<Result<V, E>>.getAllErrors(): List<E> {
*
* - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers)
*/
fun <V, E> partition(vararg results: Result<V, E>) = results.asIterable().partition()
fun <V, E> partition(vararg results: Result<V, E>): Pair<List<V>, List<E>> {
return results.asIterable().partition()
}

/**
* Partitions an [Iterable] of [Results][Result] into a [Pair] of [Lists][List]. All the [Ok]
* elements are extracted, in order, to the [first][Pair.first] value. Similarly the [Err]
* elements are extracted to the [Pair.second] value.
* elements are extracted, in order, to the [first][Pair.first] value. Similarly the [Err] elements
* are extracted to the [Pair.second] value.
*
* - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers)
*/
Expand All @@ -139,8 +139,9 @@ fun <V, E> Iterable<Result<V, E>>.partition(): Pair<List<V>, List<E>> {
}

/**
* 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.
* 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 ->
Expand All @@ -154,8 +155,9 @@ fun <V, E, U> Iterable<V>.mapResult(transform: (V) -> Result<U, E>): Result<List
}

/**
* 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.
* 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 ->
Expand All @@ -169,8 +171,9 @@ fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo(destination:
}

/**
* 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.
* 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 ->
Expand All @@ -185,8 +188,9 @@ fun <V, E, U : Any> Iterable<V>.mapResultNotNull(transform: (V) -> Result<U, E>?
}

/**
* 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.
* 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 ->
Expand All @@ -201,8 +205,9 @@ fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultNotNullTo(
}

/**
* 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.
* 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 ->
Expand All @@ -216,8 +221,9 @@ fun <V, E, U> Iterable<V>.mapResultIndexed(transform: (index: Int, V) -> Result<
}

/**
* 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.
* 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 ->
Expand All @@ -231,9 +237,9 @@ fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedTo(destin
}

/**
* 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.
* 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 ->
Expand All @@ -248,8 +254,9 @@ fun <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull(transform: (index: Int,
}

/**
* 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.
* 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 ->
Expand Down
37 changes: 16 additions & 21 deletions src/main/kotlin/com/github/michaelbull/result/Map.kt
@@ -1,8 +1,8 @@
package com.github.michaelbull.result

/**
* Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform] function
* to the [value][Ok.value] if this [Result] is [Ok], or returning this [Err].
* Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform]
* function to the [value][Ok.value] if this [Result] is [Ok], or returning this [Err].
*
* - Elm: [Result.map](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map)
* - Haskell: [Data.Bifunctor.first](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:first)
Expand All @@ -16,8 +16,8 @@ inline infix fun <V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E> {
}

/**
* Maps this [Result<V, E>][Result] to [Result<V, F>][Result] by either applying the [transform] function
* to the [error][Err.error] if this [Result] is [Err], or returning this [Ok].
* Maps this [Result<V, E>][Result] to [Result<V, F>][Result] by either applying the [transform]
* function to the [error][Err.error] if this [Result] is [Err], or returning this [Ok].
*
* - Elm: [Result.mapError](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#mapError)
* - Haskell: [Data.Bifunctor.right](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:second)
Expand All @@ -31,8 +31,9 @@ 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.
* 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 ->
Expand All @@ -48,42 +49,36 @@ fun <V, E, U> Result<Iterable<V>, E>.mapAll(transform: (V) -> Result<U, E>): Res
}

/**
* 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
* return the same type (`U`).
* 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 return the same type (`U`).
*
* - Elm: [Result.Extra.mapBoth](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#mapBoth)
* - Haskell: [Data.Either.either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:either)
*/
inline fun <V, E, U> Result<V, E>.mapBoth(
success: (V) -> U,
failure: (E) -> U
): U {
inline fun <V, E, U> Result<V, E>.mapBoth(success: (V) -> U, failure: (E) -> U): U {
return when (this) {
is Ok -> success(value)
is Err -> failure(error)
}
}

/**
* Maps this [Result<V, E>][Result] to [Result<U, F>][Result] by applying either the [success] function
* if this [Result] is [Ok], or the [failure] function if this [Result] is an [Err].
* Maps this [Result<V, E>][Result] to [Result<U, F>][Result] by applying either the [success]
* function if this [Result] is [Ok], or the [failure] function if this [Result] is an [Err].
*
* - Haskell: [Data.Bifunctor.Bimap](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:bimap)
*/
inline fun <V, E, U, F> Result<V, E>.mapEither(
success: (V) -> U,
failure: (E) -> F
): Result<U, F> {
inline fun <V, E, U, F> Result<V, E>.mapEither(success: (V) -> U, failure: (E) -> F): Result<U, F> {
return when (this) {
is Ok -> Ok(success(value))
is Err -> Err(failure(error))
}
}

/**
* Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform] function
* if this [Result] is [Ok], or returning this [Err].
* Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform]
* function if this [Result] is [Ok], or returning this [Err].
*
* This is functionally equivalent to [andThen].
*
Expand Down

0 comments on commit 1377350

Please sign in to comment.