arrow-refined-types / arrow.refinement.booleans / Or

Or

class Or<A, B> : Refined<A, B>

Boolean disjunction of the left and right predicates. left or right

In the example below we can see how we can use or to compose the predicates of arrow.refinement.numbers.PositiveInt and arrow.refinement.numbers.Even

import arrow.refinement.Refined
import arrow.refinement.numbers.PositiveInt
import arrow.refinement.numbers.Even

@JvmInline
value class PositiveOrEven /* private constructor */(val value: Int) {
  companion object:
    Refined<Int, PositiveOrEven>(::PositiveOrEven, PositiveInt or Even)
}

Safe nullable construction

PositiveOrEven.orNull(2)
// PositiveOrEven(value=2)
PositiveOrEven.orNull(1)
// PositiveOrEven(value=1)
PositiveOrEven.orNull(-2)
// PositiveOrEven(value=-2)

Resolved constraints

PositiveOrEven.constraints(2)
// [(true, 2 should be > 0), (true, 2 should be even)]
PositiveOrEven.constraints(-1)
// [(false, -1 should be > 0), (false, -1 should be even)]
PositiveOrEven.isValid(2)
// true
PositiveOrEven.isValid(-1)
// false

Folding validation

PositiveOrEven.fold(2, { "failed: $it" }, { "success: $it" })
// success: PositiveOrEven(value=2)
PositiveOrEven.fold(-1, { "failed: $it" }, { "success: $it" })
// failed: [(false, -1 should be > 0), (false, -1 should be even)]
PositiveOrEven.fold(1, { "failed: $it" }, { "success: $it" })
// success: PositiveOrEven(value=1)

Unsafe require

PositiveOrEven.require(2)
// 2
try { PositiveOrEven.require(-1) } catch (e: IllegalArgumentException) { e.message }
// -1 should be > 0, -1 should be even

See Also

arrow.refinement.Refined.or

Constructors

<init> Boolean disjunction of the left and right predicates. left or rightOr(left: Refined<A, *>, right: Refined<A, B>, defaultMsg: String? = null)

Do you like Arrow?

Arrow Org
<