filter Or Other
inline fun <A, B> Either<A, B>.filterOrOther(predicate: (B) -> Boolean, default: (B) -> A): Either<A, B>
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({right}))
if this is a Right and the given predicate does not hold for the right value. Useful for error handling where 'default' returns a message with context on why the value did not pass the filter
Returns Left with the existing value of Left if this is a Left.
Example:
import arrow.core.*
suspend fun main(): Unit {
//sampleStart
Either.Right(7).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" })
.let(::println) // Either.Left(Value '7' is not equal to 10")
Either.Right(10).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" })
.let(::println) // Either.Right(10)
Either.Left(12).filterOrOther({ str: String -> str.contains("impossible") }, { -1 })
.let(::println) // Either.Left(12)
//sampleEnd
}
Content copied to clipboard