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

NotEmpty

class NotEmpty

NotEmpty constrains a collection to be not empty

Safe nullable construction

import arrow.refinement.collections.NotEmpty

NotEmpty.orNull(listOf(1, 2))
// [1, 2]
NotEmpty.orNull(emptyList<Int>())

Resolved constraints

NotEmpty.constraints(listOf(1, 2))
// [(true, Expected non empty iterable but found [1, 2])]
NotEmpty.constraints(emptyList<Int>())
// [(false, Expected non empty iterable but found [])]
NotEmpty.isValid(listOf(1, 2))
// true
NotEmpty.isValid(emptyList<Int>())
// false

Folding validation

NotEmpty.fold(listOf(1, 2), { "failed: $it" }, { "success: $it" })
// success: NotEmpty(value=[1, 2])
NotEmpty.fold(emptyList<Int>(), { "failed: $it" }, { "success: $it" })
// failed: [(false, Expected non empty iterable but found [])]

Unsafe require

NotEmpty.require(listOf(2, 3))
// [2, 3]
try { NotEmpty.require(emptyList<Int>()) } catch (e: IllegalArgumentException) { e.message }
// Expected non empty iterable but found []

Types

Companion companion object Companion : Refined<Iterable<*>, NotEmpty>

Properties

value val value: Iterable<*>

Do you like Arrow?

Arrow Org
<