arrow-refined-types / arrow.refinement.numbers / DivisibleBy

DivisibleBy

class DivisibleBy

DivisibleBy constrains an Int to be divisible by N

Safe nullable construction

import arrow.refinement.numbers.DivisibleBy

DivisibleBy.N(2u).orNull(2)
// DivisibleBy(value=2)
DivisibleBy.N(2u).orNull(1)

Resolved constraints

DivisibleBy.N(2u).constraints(2)
// [(true, 2 should be divisible by 2)]
DivisibleBy.N(2u).constraints(1)
// [(false, 1 should be divisible by 2)]
DivisibleBy.N(2u).isValid(2)
// true
DivisibleBy.N(2u).isValid(1)
// false

Folding validation

DivisibleBy.N(2u).fold(2, { "failed: $it" }, { "success: $it" })
// success: DivisibleBy(value=2)
DivisibleBy.N(2u).fold(1, { "failed: $it" }, { "success: $it" })
// failed: [(false, 1 should be divisible by 2)]

Unsafe require

DivisibleBy.N(2u).require(2)
// 2
try { DivisibleBy.N(2u).require(1) } catch (e: IllegalArgumentException) { e.message }
// 1 should be divisible by 2

Types

N class N : Refined<Int, DivisibleBy>

Properties

value val value: Int

Do you like Arrow?

Arrow Org
<