NonEmptyList

A type which represents a list that is not empty. NonEmptyList has no runtime, during execution it's a regular array. This gives not only performance benefits, but it also allows you to use Array and NonEmptyList interchangeably - you can pass a NonEmptyList to any function that expects an Array (this includes access to all Array.prototype methods like map and filter for free).
How to import
import { NonEmptyList } from 'purify-ts/NonEmptyList'

Constructors

NonEmptyList
<T extends NonEmptyListCompliant<T[number]>>(list: T): NonEmptyList<T[number]>
Typecasts an array with at least one element into a `NonEmptyList`. Works only if the compiler can confirm that the array has one or more elements.
NonEmptyList([1])
NonEmptyList([])
// NonEmptyList<number>
// Compiler error

Static methods

fromArray
[a] -> Maybe (NonEmptyList a)<T>(source: T[]): Maybe<NonEmptyList<T>>
Returns a `Just NonEmptyList` if the parameter has one or more elements, otherwise it returns `Nothing`.
NonEmptyList.fromArray([1])
NonEmptyList.fromArray([])
Just(NonEmptyList([1]))
Nothing
fromTuple
<T, U>(source: Tuple<T, U>): NonEmptyList<T | U>
Converts a `Tuple` to a `NonEmptyList`.
NonEmptyList.fromTuple(Tuple(1, 2))
NonEmptyList([1, 2])
unsafeCoerce
<T>(source: T[]): NonEmptyList<T>
Typecasts any array into a `NonEmptyList`, but throws an exception if the array is empty. Use `fromArray` as a safe alternative.
NonEmptyList.unsafeCoerce([])
NonEmptyList.unsafeCoerce([1])
// Uncaught Error: NonEmptyList#unsafeCoerce was ran on an empty array
// NonEmptyList<number>
isNonEmpty
[a] -> Bool<T>(list: T[]): list is NonEmptyList<T>
Returns true and narrows the type if the passed array has one or more elements.
NonEmptyList.isNonEmpty([1])
true
head
NonEmptyList a -> a<T>(list: NonEmptyList<T>): T
The same function as `List#head`, but it doesn't return a Maybe as a NonEmptyList will always have a head.
last
NonEmptyList a -> a<T>(list: NonEmptyList<T>): T
The same function as `List#last`, but it doesn't return a Maybe as a NonEmptyList will always have a last element.
tail
NonEmptyList a -> [a]<T>(list: NonEmptyList<T>): T[]
The same function as `List#tail`, but it doesn't return a Maybe as a NonEmptyList will always have a tail (although it may be of length 0).