Consistently wrap documentation at 100 characters

This commit is contained in:
Michael Bull 2018-09-18 20:00:50 +01:00
parent 29e21e57a0
commit 1377350895
4 changed files with 64 additions and 62 deletions

View File

@ -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 * Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform]
* if this [Result] is [Ok], or returning this [Err]. * 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) * - 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) * - Rust: [Result.and_then](https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then)

View File

@ -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 * Returns the [value][Ok.value] if this [Result] is [Ok], otherwise the
* the [transformation][transform] of the [error][Err.error]. * [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) * - 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) * - Rust: [Result.unwrap_or_else](https://doc.rust-lang.org/src/core/result.rs.html#735-740)
@ -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 * Returns the [error][Err.error] if this [Result] is [Err], otherwise the
* the [transformation][transform] of the [value][Ok.value]. * [transformation][transform] of the [value][Ok.value].
*/ */
inline infix fun <V, E> Result<V, E>.getErrorOrElse(transform: (V) -> E): E { inline infix fun <V, E> Result<V, E>.getErrorOrElse(transform: (V) -> E): E {
return when (this) { return when (this) {

View File

@ -1,12 +1,10 @@
package com.github.michaelbull.result 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( inline fun <T, R, E> Iterable<T>.fold(initial: R, operation: (acc: R, T) -> Result<R, E>): Result<R, E> {
initial: R,
operation: (acc: R, T) -> Result<R, E>
): Result<R, E> {
var accumulator = initial var accumulator = initial
forEach { element -> forEach { element ->
@ -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( inline fun <T, R, E> List<T>.foldRight(initial: R, operation: (T, acc: R) -> Result<R, E>): Result<R, E> {
initial: R,
operation: (T, acc: R) -> Result<R, E>
): Result<R, E> {
var accumulator = initial var accumulator = initial
if (!isEmpty()) { if (!isEmpty()) {
@ -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) * - 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 * Extracts from an [Iterable] of [Results][Result] all the [Ok] elements. All the [Ok] elements
@ -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 * Extracts from a vararg of [Results][Result] all the [Err] elements. All the [Err] elements are
* are extracted in order. * extracted in order.
* *
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights) * - 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() 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] * Extracts from an [Iterable] of [Results][Result] all the [Err] elements. All the [Err] elements
* elements are extracted in order. * are extracted in order.
* *
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights) * - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
*/ */
@ -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) * - 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] * 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, in order, to the [first][Pair.first] value. Similarly the [Err] elements
* elements are extracted to the [Pair.second] value. * 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) * - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers)
*/ */
@ -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 * Returns a [Result<List<U>, E>][Result] containing the results of applying the given [transform]
* element in the original collection, returning early with the first [Err] if a transformation fails. * 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> { fun <V, E, U> Iterable<V>.mapResult(transform: (V) -> Result<U, E>): Result<List<U>, E> {
return Ok(map { element -> return Ok(map { element ->
@ -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 * Applies the given [transform] function to each element of the original collection and appends
* given [destination], returning early with the first [Err] if a transformation fails. * 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> { 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 ->
@ -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] * Returns a [Result<List<U>, E>][Result] containing only the non-null results of applying the
* function to each element in the original collection, returning early with the first [Err] if a transformation fails. * 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> { 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 ->
@ -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 * Applies the given [transform] function to each element in the original collection and appends
* results to the given [destination], returning early with the first [Err] if a transformation fails. * 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> { 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 ->
@ -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 * Returns a [Result<List<U>, E>][Result] containing the results of applying the given [transform]
* element and its index in the original collection, returning early with the first [Err] if a transformation fails. * 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> { 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 ->
@ -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 * Applies the given [transform] function to each element and its index in the original collection
* results to the given [destination], returning early with the first [Err] if a transformation fails. * 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> { 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 ->
@ -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] * Returns a [Result<List<U>, E>][Result] containing only the non-null results of applying the
* function to each element and its index in the original collection, returning early with the first [Err] if a * given [transform] function to each element and its index in the original collection, returning
* transformation fails. * 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> { 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 ->
@ -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 * Applies the given [transform] function to each element and its index in the original collection
* non-null results to the given [destination], returning early with the first [Err] if a transformation fails. * 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> { 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 ->

View File

@ -1,8 +1,8 @@
package com.github.michaelbull.result package com.github.michaelbull.result
/** /**
* Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform] function * Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform]
* to the [value][Ok.value] if this [Result] is [Ok], or returning this [Err]. * 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) * - 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) * - Haskell: [Data.Bifunctor.first](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:first)
@ -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 * Maps this [Result<V, E>][Result] to [Result<V, F>][Result] by either applying the [transform]
* to the [error][Err.error] if this [Result] is [Err], or returning this [Ok]. * 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) * - 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) * - Haskell: [Data.Bifunctor.right](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:second)
@ -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 * Returns a [Result<List<U>, E>][Result] containing the results of applying the given [transform]
* element in the original collection, returning early with the first [Err] if a transformation fails. * 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> { fun <V, E, U> Result<Iterable<V>, E>.mapAll(transform: (V) -> Result<U, E>): Result<List<U>, E> {
return map { iterable -> return map { iterable ->
@ -48,17 +49,14 @@ 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] * Maps this [Result<V, E>][Result] to `U` by applying either the [success] function if this
* is [Ok], or the [failure] function if this [Result] is an [Err]. Both of these functions must * [Result] is [Ok], or the [failure] function if this [Result] is an [Err]. Both of these
* return the same type (`U`). * 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) * - 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) * - 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( inline fun <V, E, U> Result<V, E>.mapBoth(success: (V) -> U, failure: (E) -> U): U {
success: (V) -> U,
failure: (E) -> U
): U {
return when (this) { return when (this) {
is Ok -> success(value) is Ok -> success(value)
is Err -> failure(error) is Err -> failure(error)
@ -66,15 +64,12 @@ inline fun <V, E, U> Result<V, E>.mapBoth(
} }
/** /**
* Maps this [Result<V, E>][Result] to [Result<U, F>][Result] by applying either the [success] function * Maps this [Result<V, E>][Result] to [Result<U, F>][Result] by applying either the [success]
* if this [Result] is [Ok], or the [failure] function if this [Result] is an [Err]. * 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) * - 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( inline fun <V, E, U, F> Result<V, E>.mapEither(success: (V) -> U, failure: (E) -> F): Result<U, F> {
success: (V) -> U,
failure: (E) -> F
): Result<U, F> {
return when (this) { return when (this) {
is Ok -> Ok(success(value)) is Ok -> Ok(success(value))
is Err -> Err(failure(error)) is Err -> Err(failure(error))
@ -82,8 +77,8 @@ inline fun <V, E, U, F> Result<V, E>.mapEither(
} }
/** /**
* Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform] function * Maps this [Result<V, E>][Result] to [Result<U, E>][Result] by either applying the [transform]
* if this [Result] is [Ok], or returning this [Err]. * function if this [Result] is [Ok], or returning this [Err].
* *
* This is functionally equivalent to [andThen]. * This is functionally equivalent to [andThen].
* *