From 1377350895c7a7e45a042308be39dc48feb368da Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Tue, 18 Sep 2018 20:00:50 +0100 Subject: [PATCH] Consistently wrap documentation at 100 characters --- .../com/github/michaelbull/result/And.kt | 4 +- .../com/github/michaelbull/result/Get.kt | 8 +- .../com/github/michaelbull/result/Iterable.kt | 77 ++++++++++--------- .../com/github/michaelbull/result/Map.kt | 37 ++++----- 4 files changed, 64 insertions(+), 62 deletions(-) diff --git a/src/main/kotlin/com/github/michaelbull/result/And.kt b/src/main/kotlin/com/github/michaelbull/result/And.kt index 6fc62bb..6ccf307 100644 --- a/src/main/kotlin/com/github/michaelbull/result/And.kt +++ b/src/main/kotlin/com/github/michaelbull/result/And.kt @@ -18,8 +18,8 @@ inline infix fun Result.and(result: () -> Result): Result][Result] to [Result][Result] by either applying the [transform] function - * if this [Result] is [Ok], or returning this [Err]. + * Maps this [Result][Result] to [Result][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) diff --git a/src/main/kotlin/com/github/michaelbull/result/Get.kt b/src/main/kotlin/com/github/michaelbull/result/Get.kt index e520d6d..76227d1 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Get.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Get.kt @@ -68,8 +68,8 @@ inline infix fun Result.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) @@ -82,8 +82,8 @@ inline infix fun Result.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 Result.getErrorOrElse(transform: (V) -> E): E { return when (this) { diff --git a/src/main/kotlin/com/github/michaelbull/result/Iterable.kt b/src/main/kotlin/com/github/michaelbull/result/Iterable.kt index 8a7e5de..83795fb 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Iterable.kt +++ b/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 Iterable.fold( - initial: R, - operation: (acc: R, T) -> Result -): Result { +inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> Result): Result { var accumulator = initial forEach { element -> @@ -24,12 +22,10 @@ inline fun Iterable.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 List.foldRight( - initial: R, - operation: (T, acc: R) -> Result -): Result { +inline fun List.foldRight(initial: R, operation: (T, acc: R) -> Result): Result { var accumulator = initial if (!isEmpty()) { @@ -78,7 +74,9 @@ fun Iterable>.combine(): Result, E> { * * - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts) */ -fun getAll(vararg results: Result) = results.asIterable().getAll() +fun getAll(vararg results: Result): List { + return results.asIterable().getAll() +} /** * Extracts from an [Iterable] of [Results][Result] all the [Ok] elements. All the [Ok] elements @@ -91,16 +89,16 @@ fun Iterable>.getAll(): List { } /** - * 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 getAllErrors(vararg results: Result) = 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) */ @@ -115,12 +113,14 @@ fun Iterable>.getAllErrors(): List { * * - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers) */ -fun partition(vararg results: Result) = results.asIterable().partition() +fun partition(vararg results: Result): Pair, List> { + 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) */ @@ -139,8 +139,9 @@ fun Iterable>.partition(): Pair, List> { } /** - * 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. + * 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 -> @@ -154,8 +155,9 @@ fun Iterable.mapResult(transform: (V) -> Result): Result> Iterable.mapResultTo(destination: C, transform: (V) -> Result): Result { return Ok(mapTo(destination) { element -> @@ -169,8 +171,9 @@ fun > Iterable.mapResultTo(destination: } /** - * 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. + * 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 -> @@ -185,8 +188,9 @@ fun Iterable.mapResultNotNull(transform: (V) -> Result? } /** - * 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 > Iterable.mapResultNotNullTo(destination: C, transform: (V) -> Result?): Result { return Ok(mapNotNullTo(destination) { element -> @@ -201,8 +205,9 @@ fun > Iterable.mapResultNotNullTo( } /** - * 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. + * 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 -> @@ -216,8 +221,9 @@ fun Iterable.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 > Iterable.mapResultIndexedTo(destination: C, transform: (index: Int, V) -> Result): Result { return Ok(mapIndexedTo(destination) { index, element -> @@ -231,9 +237,9 @@ fun > Iterable.mapResultIndexedTo(destin } /** - * 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. + * 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 -> @@ -248,8 +254,9 @@ fun Iterable.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 > Iterable.mapResultIndexedNotNullTo(destination: C, transform: (index: Int, V) -> Result?): Result { return Ok(mapIndexedNotNullTo(destination) { index, element -> diff --git a/src/main/kotlin/com/github/michaelbull/result/Map.kt b/src/main/kotlin/com/github/michaelbull/result/Map.kt index ec18c54..60ada99 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Map.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Map.kt @@ -1,8 +1,8 @@ package com.github.michaelbull.result /** - * Maps this [Result][Result] to [Result][Result] by either applying the [transform] function - * to the [value][Ok.value] if this [Result] is [Ok], or returning this [Err]. + * Maps this [Result][Result] to [Result][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) @@ -16,8 +16,8 @@ inline infix fun Result.map(transform: (V) -> U): Result { } /** - * Maps this [Result][Result] to [Result][Result] by either applying the [transform] function - * to the [error][Err.error] if this [Result] is [Err], or returning this [Ok]. + * Maps this [Result][Result] to [Result][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) @@ -31,8 +31,9 @@ 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. + * 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 Result, E>.mapAll(transform: (V) -> Result): Result, E> { return map { iterable -> @@ -48,17 +49,14 @@ fun Result, E>.mapAll(transform: (V) -> Result): Res } /** - * 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 - * return the same type (`U`). + * 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 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 Result.mapBoth( - success: (V) -> U, - failure: (E) -> U -): U { +inline fun Result.mapBoth(success: (V) -> U, failure: (E) -> U): U { return when (this) { is Ok -> success(value) is Err -> failure(error) @@ -66,15 +64,12 @@ inline fun Result.mapBoth( } /** - * Maps this [Result][Result] to [Result][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][Result] to [Result][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 Result.mapEither( - success: (V) -> U, - failure: (E) -> F -): Result { +inline fun Result.mapEither(success: (V) -> U, failure: (E) -> F): Result { return when (this) { is Ok -> Ok(success(value)) is Err -> Err(failure(error)) @@ -82,8 +77,8 @@ inline fun Result.mapEither( } /** - * Maps this [Result][Result] to [Result][Result] by either applying the [transform] function - * if this [Result] is [Ok], or returning this [Err]. + * Maps this [Result][Result] to [Result][Result] by either applying the [transform] + * function if this [Result] is [Ok], or returning this [Err]. * * This is functionally equivalent to [andThen]. *