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

MinSize

class MinSize

MinSize constrains a collection to be have a minimum N number of elements

Safe nullable construction

import arrow.refinement.collections.MinSize

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

Resolved constraints

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

Folding validation

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

Unsafe require

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

Types

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

Properties

value val value: Iterable<*>

Do you like Arrow?

Arrow Org
<