arrow-refined-types / arrow.refinement.collections / Count

Count

class Count

Count constrains a collection to be have N number of elements

Safe nullable construction

import arrow.refinement.collections.Count

Count.N(1u).orNull(listOf(1, 2))
Count.N(1u).orNull(listOf(1))
// [1]

Resolved constraints

Count.N(1u).constraints(listOf(1, 2))
// [(false, Expected count to be 1)]
Count.N(1u).constraints(listOf(1))
// [(true, Expected count to be 1)]
Count.N(1u).isValid(listOf(1, 2))
// false
Count.N(1u).isValid(listOf(1))
// true

Folding validation

Count.N(1u).fold(listOf(1, 2), { "failed: $it" }, { "success: $it" })
// failed: [(false, Expected count to be 1)]
Count.N(1u).fold(listOf(1), { "failed: $it" }, { "success: $it" })
// success: Count(value=[1])

Unsafe require

Count.N(1u).require(listOf(1))
// [1]
try { Count.N(1u).require(listOf(2, 3)) } catch (e: IllegalArgumentException) { e.message }
// Expected count to be 1

Types

N class N : Refined<Iterable<*>, Count>

Properties

value val value: Iterable<*>

Do you like Arrow?

Arrow Org
<