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

FromTo

class FromTo

FromTo constrains Int to be in range of From to To

Safe nullable construction

import arrow.refinement.numbers.FromTo

FromTo.N(1u, 5u).orNull(2)
// FromTo(value=2)
FromTo.N(1u, 5u).orNull(0)

Resolved constraints

FromTo.N(1u, 5u).constraints(2)
// [(true, 2 should be in range 1..5)]
FromTo.N(1u, 5u).constraints(0)
// [(false, 0 should be in range 1..5), (true, 0 should be in range 1..5)]
FromTo.N(1u, 5u).isValid(2)
// true
FromTo.N(1u, 5u).isValid(0)
// false

Folding validation

FromTo.N(1u, 5u).fold(2, { "failed: $it" }, { "success: $it" })
// success: FromTo(value=2)
FromTo.N(1u, 5u).fold(0, { "failed: $it" }, { "success: $it" })
// failed: [(false, 0 should be in range 1..5)]

Unsafe require

FromTo.N(1u, 5u).require(2)
// 2
try { FromTo.N(1u, 5u).require(0) } catch (e: IllegalArgumentException) { e.message }
// 0 should be in range 1..5

Types

N class N : Refined<Int, FromTo>

Properties

value val value: Int

Do you like Arrow?

Arrow Org
<