diff --git a/src/main/kotlin/com/github/michaelbull/result/Map.kt b/src/main/kotlin/com/github/michaelbull/result/Map.kt index 2394da1..61c4819 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Map.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Map.kt @@ -166,3 +166,18 @@ inline infix fun Result.flatMap(transform: (V) -> Result): return andThen(transform) } + +/** + * Returns the [transformation][transform] of the [value][Ok.value] if this [Result] is [Ok] + * and satisfies the given [predicate], otherwise this [Result]. + */ +inline fun Result.toErrorIf(predicate: (V) -> Boolean, transform: (V) -> E): Result { + return when (this) { + is Ok -> if (predicate(value)) { + Err(transform(value)) + } else { + this + } + is Err -> this + } +}