//arrow-core/arrow.core/filterOrElse

filterOrElse

common inline fun <A, B> Either<A, B>.filterOrElse(predicate: (B) -> Boolean, default: () -> A): Either<A, B>

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)
}

Do you like Arrow?

Arrow Org
<