filter Or Else
inline fun <A, B> Either<A, B>.filterOrElse(predicate: (B) -> Boolean, default: () -> A): Either<A, B>
Deprecated
This API is considered redundant. If this method is crucial for you, please let us know on the Arrow Github. Thanks! https://github.com/arrow-kt/arrow/issues Prefer if-else statement inside either DSL, or replace with explicit flatMap
Replace with
flatMap { b -> b.takeIf(predicate)?.right() ?: default().left() }Content copied to clipboard
Returns Right with the existing value of Right if this is a Right and the given predicate holds for the right value.
Returns Left(default) if this is a Right and the given predicate does not hold for the right value.
Returns Left with the existing value of Left if this is a Left.
Example:
import arrow.core.Either.*
import arrow.core.Either
import arrow.core.filterOrElse
fun main() {
Right(12).filterOrElse({ it 10 }, { -1 }) // Result: Right(12)
Right(7).filterOrElse({ it 10 }, { -1 }) // Result: Left(-1)
val left: Either<Int, Int> = Left(12)
left.filterOrElse({ it 10 }, { -1 }) // Result: Left(12)
}Content copied to clipboard