Abstract around NonEmptyList<A>, which is similar to a Cons-style list, but must contain at least one element (cannot be empty).

Methods

append (nel:Nel<A>):Nel<A>

Appends another non-empty list to this Nel<A>.

Warning: this operation is O(n)

flatMap<B> (f:A ‑> Nel<B>):Nel<B>

Applies an A -> Nel<B> function to each element in this Nel<A> and flattens the result to create a new Nel<B>

fold (f:A ‑> A ‑> A):A

Applies a reducing function to this Nel<A>

head ():A

Gets the head item of this Nel<A>, which is guaranteed to exist

init ():ReadonlyArray<A>

Gets the initial elements (all but the last element) of the Nel<A> as a possibly-empty ReadonlyArray<A>

Warning: this operation is O(n)

last ():A

Gets the last item of the Nel<A>, which is guaranteed to exist.

Warning: this operation is O(n)

map<B> (f:A ‑> B):Nel<B>

Applies an A -> B function to each element in this Nel<A> to create a new Nel<B>

pop ():Tuple<A, ReadonlyArray<A>>

Returns the last item of the Nel<A> and a new Nel<A> with the last item removed.

Does not modify this Nel<A>.

Warning: this operation is O(n)

push (a:A):Nel<A>

Returns a new Nel<A> with the given item added at the end.

Does not modify this Nel<A>.

Warning: this operation is O(n)

shift ():Tuple<A, ReadonlyArray<A>>

Returns the first item of the Nel<A> and a new Nel<A> with the first item removed.

Does not modify this Nel<A>.

tail ():ReadonlyArray<A>

Gets the tail (all but the first element) of the Nel<A> as a possibly-empty ReadonlyArray<A>

toArray ():ReadonlyArray<A>

Converts the Nel<A> to a ReadonlyArray<A>

Warning: this operation is O(n)

unshift (a:A):Nel<A>

Returns a new Nel<A> with the given item added at the front.

Does not modify this Nel<A>.

Static methods

staticcons<A> (a:A, nl:Nel<A>):Nel<A>

Constructs a Nel<A> from a head element and tail Nel<A>

staticfromArray<A> (arr:ReadonlyArray<A>):Option<Nel<A>>

Attempts to construct a Nel<A> from a possibly-empty Array<A>. If the array is empty, None is returned.

staticnel<A> (hd:A, tl:Array<A>):Nel<A>

Constructs a Nel<A> from a head element and tail Array<A>

staticpure<A> (a:A):Nel<A>

Constructs a Nel<A> from a head element

staticsemigroup<A> ():Semigroup<Nel<A>>

Gets a Semigroup instance for Nel<A>, using the append method of Nel<A>.