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

Contains

class Contains<A>

Contains constrains a collection to include an element

Safe nullable construction

import arrow.refinement.collections.Contains

Contains.Element(1).orNull(listOf(1, 2))
// [1, 2]
Contains.Element(1).orNull(listOf(2, 3))

Resolved constraints

Contains.Element(1).constraints(listOf(1, 2))
// [(true, Expected [1, 2] to contain 1)]
Contains.Element(1).constraints(listOf(2, 3))
// [(false, Expected [2, 3] to contain 1)]
Contains.Element(1).isValid(listOf(1, 2))
// true
Contains.Element(1).isValid(listOf(2, 3))
// false

Folding validation

Contains.Element(1).fold(listOf(1, 2), { "failed: $it" }, { "success: $it" })
// success: Contains(value=[1, 2])
Contains.Element(1).fold(listOf(2, 3), { "failed: $it" }, { "success: $it" })
// failed: [(false, Expected [2, 3] to contain 1)]

Unsafe require

Contains.Element(1).require(listOf(1, 2))
// [1, 2]
try { Contains.Element(1).require(listOf(2, 3)) } catch (e: IllegalArgumentException) { e.message }
// Expected [2, 3] to contain 1

Types

Element class Element<A> : Refined<Iterable<A>, Contains<A>>

Properties

value val value: Iterable<A>

Do you like Arrow?

Arrow Org
<