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

MaxSize

class MaxSize

MaxSize constrains a collection to be have a maximum N number of elements

Safe nullable construction

import arrow.refinement.collections.MaxSize

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

Resolved constraints

MaxSize.N(1u).constraints(listOf(1, 2))
// [(false, Expected max size of 1 but found 2)]
MaxSize.N(1u).constraints(listOf(1))
// [(true, Expected max size of 1 but found 1)]
MaxSize.N(1u).isValid(listOf(1, 2))
// false
MaxSize.N(1u).isValid(listOf(1))
// true

Folding validation

MaxSize.N(1u).fold(listOf(1, 2), { "failed: $it" }, { "success: $it" })
// failed: [(false, Expected max size of 1 but found 2)]
MaxSize.N(1u).fold(listOf(1), { "failed: $it" }, { "success: $it" })
// success: MaxSize(value=[1])

Unsafe require

MaxSize.N(1u).require(listOf(1))
// [1]
try { MaxSize.N(1u).require(listOf(2, 3)) } catch (e: IllegalArgumentException) { e.message }
// Expected max size of 1 but found 2

Types

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

Properties

value val value: Iterable<*>

Do you like Arrow?

Arrow Org
<