NonEmptyList

class NonEmptyList<out A>(val head: A, val tail: List<A>) : AbstractList<A>

NonEmptyList is a data type used in Λrrow to model ordered lists that guarantee to have at least one value.

Constructing NonEmptyList

A NonEmptyList guarantees the list always has at least 1 element.

import arrow.core.nonEmptyListOf
import arrow.core.toNonEmptyListOrNull

fun main() {
println(nonEmptyListOf(1, 2, 3, 4, 5))
println(listOf(1, 2, 3).toNonEmptyListOrNull())
println(emptyList<Int>().toNonEmptyListOrNull())
}
NonEmptyList(1, 2, 3, 4, 5)
NonEmptyList(1, 2, 3)
null

head

Unlike List[0], NonEmptyList.head it's a safe operation that guarantees no exception throwing.

import arrow.core.nonEmptyListOf

val value =
//sampleStart
nonEmptyListOf(1, 2, 3, 4, 5).head
//sampleEnd
fun main() {
println(value)
}

foldLeft

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.

import arrow.core.NonEmptyList
import arrow.core.nonEmptyListOf

//sampleStart
fun sumNel(nel: NonEmptyList<Int>): Int =
nel.foldLeft(0) { acc, n -> acc + n }
val value = sumNel(nonEmptyListOf(1, 1, 1, 1))
//sampleEnd
fun main() {
println("value = $value")
}

map

map allows us to transform A into B in NonEmptyList< A >

import arrow.core.nonEmptyListOf

val value =
//sampleStart
nonEmptyListOf(1, 1, 1, 1).map { it + 1 }
//sampleEnd
fun main() {
println(value)
}

Combining NonEmptyLists

flatMap

flatMap allows us to compute over the contents of multiple NonEmptyList< * > values

import arrow.core.NonEmptyList
import arrow.core.nonEmptyListOf

//sampleStart
val nelOne: NonEmptyList<Int> = nonEmptyListOf(1, 2, 3)
val nelTwo: NonEmptyList<Int> = nonEmptyListOf(4, 5)

val value = nelOne.flatMap { one ->
nelTwo.map { two ->
one + two
}
}
//sampleEnd
fun main() {
println("value = $value")
}

zip

Λrrow contains methods that allow you to preserve type information when computing over different NonEmptyList typed values.

import arrow.core.NonEmptyList
import arrow.core.nonEmptyListOf
import arrow.core.zip
import java.util.UUID

//sampleStart
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)

val value = nelId.zip(nelName, nelYear) { id, name, year ->
Person(id, name, year)
}
//sampleEnd
fun main() {
println("value = $value")
}

Summary

  • NonEmptyList is used to model lists that guarantee at least one element

  • We can easily construct values of NonEmptyList with nonEmptyListOf

  • foldLeft, map, flatMap and others are used to compute over the internal contents of a NonEmptyList value.

  • a.zip(b, c) { ... } can be used to compute over multiple NonEmptyList values preserving type information and abstracting over arity with zip

Constructors

Link copied to clipboard
fun <out A> NonEmptyList(head: A, tail: List<A>)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun <B> align(b: NonEmptyList<B>): NonEmptyList<Ior<A, B>>
Link copied to clipboard
fun <B> coflatMap(f: (NonEmptyList<A>) -> B): NonEmptyList<B>
Link copied to clipboard
open operator override fun contains(element: A): Boolean
Link copied to clipboard
open override fun containsAll(elements: Collection<A>): Boolean
Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
fun extract(): A
Link copied to clipboard
inline fun <B> flatMap(f: (A) -> NonEmptyList<B>): NonEmptyList<B>
Link copied to clipboard
inline fun <B> foldLeft(b: B, f: (B, A) -> B): B
Link copied to clipboard
open operator override fun get(index: Int): A
Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
open override fun indexOf(element: A): Int
Link copied to clipboard
open override fun isEmpty(): Boolean
Link copied to clipboard
open operator override fun iterator(): Iterator<A>
Link copied to clipboard
open override fun lastIndexOf(element: A): Int
Link copied to clipboard
open override fun listIterator(): ListIterator<A>
open override fun listIterator(index: Int): ListIterator<A>
Link copied to clipboard
inline fun <B> map(f: (A) -> B): NonEmptyList<B>
Link copied to clipboard
fun <B> padZip(other: NonEmptyList<B>): NonEmptyList<Pair<A?, B?>>
Link copied to clipboard
operator fun plus(a: @UnsafeVariance A): NonEmptyList<A>
operator fun plus(l: NonEmptyList<@UnsafeVariance A>): NonEmptyList<A>
operator fun plus(l: List<@UnsafeVariance A>): NonEmptyList<A>
Link copied to clipboard
fun salign(SA: Semigroup<@UnsafeVariance A>, b: NonEmptyList<@UnsafeVariance A>): NonEmptyList<A>
Link copied to clipboard
open override fun subList(fromIndex: Int, toIndex: Int): List<A>
Link copied to clipboard
fun toList(): List<A>
Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun <B> zip(fb: NonEmptyList<B>): NonEmptyList<Pair<A, B>>
inline fun <B, Z> zip(b: NonEmptyList<B>, map: (A, B) -> Z): NonEmptyList<Z>
inline fun <B, C, Z> zip(    b: NonEmptyList<B>,     c: NonEmptyList<C>,     map: (A, B, C) -> Z): NonEmptyList<Z>
inline fun <B, C, D, Z> zip(    b: NonEmptyList<B>,     c: NonEmptyList<C>,     d: NonEmptyList<D>,     map: (A, B, C, D) -> Z): NonEmptyList<Z>
inline fun <B, C, D, E, Z> zip(    b: NonEmptyList<B>,     c: NonEmptyList<C>,     d: NonEmptyList<D>,     e: NonEmptyList<E>,     map: (A, B, C, D, E) -> Z): NonEmptyList<Z>
inline fun <B, C, D, E, F, Z> zip(    b: NonEmptyList<B>,     c: NonEmptyList<C>,     d: NonEmptyList<D>,     e: NonEmptyList<E>,     f: NonEmptyList<F>,     map: (A, B, C, D, E, F) -> Z): NonEmptyList<Z>
inline fun <B, C, D, E, F, G, Z> zip(    b: NonEmptyList<B>,     c: NonEmptyList<C>,     d: NonEmptyList<D>,     e: NonEmptyList<E>,     f: NonEmptyList<F>,     g: NonEmptyList<G>,     map: (A, B, C, D, E, F, G) -> Z): NonEmptyList<Z>
inline fun <B, C, D, E, F, G, H, Z> zip(    b: NonEmptyList<B>,     c: NonEmptyList<C>,     d: NonEmptyList<D>,     e: NonEmptyList<E>,     f: NonEmptyList<F>,     g: NonEmptyList<G>,     h: NonEmptyList<H>,     map: (A, B, C, D, E, F, G, H) -> Z): NonEmptyList<Z>
inline fun <B, C, D, E, F, G, H, I, Z> zip(    b: NonEmptyList<B>,     c: NonEmptyList<C>,     d: NonEmptyList<D>,     e: NonEmptyList<E>,     f: NonEmptyList<F>,     g: NonEmptyList<G>,     h: NonEmptyList<H>,     i: NonEmptyList<I>,     map: (A, B, C, D, E, F, G, H, I) -> Z): NonEmptyList<Z>
inline fun <B, C, D, E, F, G, H, I, J, Z> zip(    b: NonEmptyList<B>,     c: NonEmptyList<C>,     d: NonEmptyList<D>,     e: NonEmptyList<E>,     f: NonEmptyList<F>,     g: NonEmptyList<G>,     h: NonEmptyList<H>,     i: NonEmptyList<I>,     j: NonEmptyList<J>,     map: (A, B, C, D, E, F, G, H, I, J) -> Z): NonEmptyList<Z>

Properties

Link copied to clipboard
val all: List<A>
Link copied to clipboard
val head: A
Link copied to clipboard
open override val size: Int
Link copied to clipboard
val tail: List<A>

Extensions

Link copied to clipboard
open override fun NonEmptyList<Any?>.combine(b: NonEmptyList<Any?>): NonEmptyList<Any?>

Combine two A values.

Link copied to clipboard
operator fun <A : Comparable<A>> NonEmptyList<A>.compareTo(other: NonEmptyList<A>): Int
Link copied to clipboard
fun <A> NonEmptyList<NonEmptyList<A>>.flatten(): NonEmptyList<A>
Link copied to clipboard
open fun NonEmptyList<Any?>.maybeCombine(b: NonEmptyList<Any?>?): NonEmptyList<Any?>
Link copied to clipboard
open operator fun NonEmptyList<Any?>.plus(b: NonEmptyList<Any?>): NonEmptyList<Any?>
Link copied to clipboard
fun <E, A> NonEmptyList<Either<E, A>>.sequence(): Either<E, NonEmptyList<A>>
fun <E, A> NonEmptyList<Validated<E, A>>.sequence(semigroup: Semigroup<E>): Validated<E, NonEmptyList<A>>
fun <A> NonEmptyList<Option<A>>.sequence(): Option<NonEmptyList<A>>
Link copied to clipboard
fun <E, A> NonEmptyList<Either<E, A>>.sequenceEither(): Either<E, NonEmptyList<A>>
Link copied to clipboard
fun <A> NonEmptyList<Option<A>>.sequenceOption(): Option<NonEmptyList<A>>
Link copied to clipboard
fun <E, A> NonEmptyList<Validated<E, A>>.sequenceValidated(semigroup: Semigroup<E>): Validated<E, NonEmptyList<A>>
Link copied to clipboard
inline fun <E, A, B> NonEmptyList<A>.traverse(f: (A) -> Either<E, B>): Either<E, NonEmptyList<B>>
inline fun <E, A, B> NonEmptyList<A>.traverse(semigroup: Semigroup<E>, f: (A) -> Validated<E, B>): Validated<E, NonEmptyList<B>>
inline fun <A, B> NonEmptyList<A>.traverse(f: (A) -> Option<B>): Option<NonEmptyList<B>>
Link copied to clipboard
inline fun <E, A, B> NonEmptyList<A>.traverseEither(f: (A) -> Either<E, B>): Either<E, NonEmptyList<B>>
Link copied to clipboard
inline fun <A, B> NonEmptyList<A>.traverseOption(f: (A) -> Option<B>): Option<NonEmptyList<B>>
Link copied to clipboard
inline fun <E, A, B> NonEmptyList<A>.traverseValidated(semigroup: Semigroup<E>, f: (A) -> Validated<E, B>): Validated<E, NonEmptyList<B>>
Link copied to clipboard
fun <A, B> NonEmptyList<Pair<A, B>>.unzip(): Pair<NonEmptyList<A>, NonEmptyList<B>>
fun <A, B, C> NonEmptyList<C>.unzip(f: (C) -> Pair<A, B>): Pair<NonEmptyList<A>, NonEmptyList<B>>