NonEmptyList
is a data type used in Λrrow to model ordered lists that have at least one value.
NonEmptyList
is available in the arrow-core-data
module under the import arrow.core.NonEmptyList
// gradle
compile "io.arrow-kt:arrow-core-data:$arrow_version"
// namespace
import arrow.core.*
A NonEmptyList
guarantees the list always has at least 1 element.
nonEmptyListOf(1, 2, 3, 4, 5) // NonEmptyList<Int>
nonEmptyListOf(1, 2) // NonEmptyList<Int>
//nonEmptyListOf() // does not compile
Unlike List#[0]
, NonEmptyList#head
is a safe operation that guarantees no exception throwing.
nonEmptyListOf(1, 2, 3, 4, 5).head
When we fold over a NonEmptyList
, we turn a NonEmptyList< A >
into B
by providing a seed value and a function that carries the state on each iteration over the elements of the list.
The first argument is a function that addresses the seed value, this can be any object of any type which will then become the resulting typed value.
The second argument is a function that takes the current state and element in the iteration and returns the new state after transformations have been applied.
fun sumNel(nel: NonEmptyList<Int>): Int =
nel.foldLeft(0) { acc, n -> acc + n }
sumNel(nonEmptyListOf(1, 1, 1, 1))
map
allows us to transform A
into B
in NonEmptyList< A >
nonEmptyListOf(1, 1, 1, 1).map { it + 1 }
flatMap
allows us to compute over the contents of multiple NonEmptyList< * >
values
val nelOne: NonEmptyList<Int> = nonEmptyListOf(1)
val nelTwo: NonEmptyList<Int> = nonEmptyListOf(2)
nelOne.flatMap { one ->
nelTwo.map { two ->
one + two
}
}
Λrrow contains methods that allow you to preserve type information when computing over different NonEmptyList
typed values.
import arrow.core.*
import java.util.*
data class Person(val id: UUID, val name: String, val year: Int)
// Note each NonEmptyList is of a different type
val nelId: NonEmptyList<UUID> = nonEmptyListOf(UUID.randomUUID(), UUID.randomUUID())
val nelName: NonEmptyList<String> = nonEmptyListOf("William Alvin Howard", "Haskell Curry")
val nelYear: NonEmptyList<Int> = nonEmptyListOf(1926, 1900)
nelId.zip(nelName, nelYear) { id, name, year ->
Person(id, name, year)
}
NonEmptyList
is used to model lists that guarantee at least one elementNonEmptyList
with nonEmptyListOf
foldLeft
, map
, flatMap
, and others are used to compute over the internal contents of a NonEmptyList
value.NonEmptyList.mapN(..) { ... }
can be used to compute over multiple NonEmptyList
values preserving type information and abstracting over arity with map
Do you like Arrow?
✖