arrow-core-data / arrow.core / filterOrOther

filterOrOther

fun <A, B> <ERROR CLASS><A, B>.filterOrOther(predicate: (B) -> Boolean, default: (B) -> A): Either<A, B>

Returns Either.Right with the existing value of Either.Right if this is a Either.Right and the given predicate holds for the right value.

Returns Left(default({right})) if this is a Either.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 Either.Left with the existing value of Either.Left if this is a Either.Left.

Example:

import arrow.core.*

Right(12).filterOrOther({ it > 10 }, { -1 })
// Right(12)
Right(7).filterOrOther({ it > 10 }, { "Value '$it' not greater than 10" })
// Left(Value '7' not greater than 10)
val left: Either<Int, Int> = Left(12)
left.filterOrOther({ it > 10 }, { -1 })
// Left(12)

Do you like Arrow?

Arrow Org
<