From 44a4467595a0ade41a47bdfffe7f934eded0a2d9 Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Sun, 17 Dec 2017 00:16:03 +0000 Subject: [PATCH] Alias flatMap function to andThen --- .../kotlin/com/github/michaelbull/result/Map.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/kotlin/com/github/michaelbull/result/Map.kt b/src/main/kotlin/com/github/michaelbull/result/Map.kt index 7aa1720..409dedc 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Map.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Map.kt @@ -78,3 +78,18 @@ inline fun Result.mapEither( is Err -> Err(failure(error)) } } + +/** + * 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]. + * + * - Scala: [Either.flatMap](http://www.scala-lang.org/api/2.12.0/scala/util/Either.html#flatMap[AA>:A,Y](f:B=>scala.util.Either[AA,Y]):scala.util.Either[AA,Y]) + * + * @param transform The transformation to apply to the [value][Ok.value]. + * @return The [transformed][transform] [Result] if [Ok], otherwise [Err]. + */ +infix inline fun Result.flatMap(transform: (V) -> Result): Result { + return andThen(transform) +}