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

First

class First<A>

First constrains a collection ensuring it first element matches Element

Safe nullable construction

import arrow.refinement.collections.First

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

Resolved constraints

First.Element(1).constraints(listOf(1, 2))
// [(true, Expected first: [1] to be 1)]
First.Element(1).constraints(listOf(2, 3))
// [(false, Expected first: [2] to be 1)]
First.Element(1).isValid(listOf(1, 2))
// true
First.Element(1).isValid(listOf(2, 3))
// false

Folding validation

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

Unsafe require

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

Types

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

Properties

value val value: Iterable<A>

Do you like Arrow?

Arrow Org
<