An array that always contains at least one element.
Additionaly, it can track a currently selected index, which is guaranteed to point to an existing element in the array.
Most functions (like map) keep the currently selected index untouched, other functions (like filter or slice) discard the currently selected and reset it to zero. If not otherwise mentioned, the selected index is kept; if it is discard, the function documentation states so explicitly.
Creates a new, non empty array with a single element. The selected index of the resulting array will be 0.
Initialize an array. initialize n f creates an array of length n with
the element at index i initialized to the result of (f i).
When the requested number of elements is smaller than one (0 or negative), the
returned array will still contain one element, f 0.
Just (initialize 4 identity) --> fromList [0, 1, 2, 3]
Just (initialize 4 (\n -> n*n)) --> fromList [0, 1, 4, 9]
Just (initialize 4 (always 0)) --> fromList [0, 0, 0, 0]
Just (initialize 0 identity) --> fromList [0]The selected index of the resulting array will be 0.
Creates an array with a given length, filled with a default element.
When the requested number of elements is smaller than one (0 or negative), this function will still return a non empty array with one element.
Just (repeat 5 0) --> fromList [0, 0, 0, 0, 0]
Just (repeat 3 "cat") --> fromList ["cat", "cat", "cat"]
Just (repeat 0 "frog") --> fromList ["frog"]Notice that repeat 3 x is the same as initialize 3 (always x).
The selected index of the resulting array will be 0.
Create a NonEmptyArray from a List.
Given an empty list, this function will return Nothing.
Given a list with at least one element, this function will return Just that
list, converted to a NonEmptyArray.
The selected index of the resulting array will be 0.
Create a NonEmptyArray from an Array.
Given an empty array, this function will return Nothing.
Given an array with at least one element, this function will return Just that
array, converted to a NonEmptyArray.
The selected index of the resulting array will be 0.
Return the length of the array.
Return Just the element at the index or Nothing if the index is out of
range.
get 0 (initialize 3 identity) --> Just 0
get 2 (initialize 3 identity) --> Just 2
get 5 (initialize 3 identity) --> Nothing
get -1 (initialize 3 identity) --> NothingNotice that in contrast to mgold/elm-nonempty-list, where head and tail do
not return Maybes, this function still necessarily returns a Maybe, because the
given index can still be out of range. But see getFirst for a function that is
known at compile time to return a value.
Return the first element of a NonEmptyArray.
getFirst (initialize 3 identity) --> 0Return the element at the selected index.
initialize 3 identity
|> setSelectedIndex 1
|> getSelected
--> 1Return the selected index.
repeat 5 "x"
|> setSelectedIndex 3
|> selectedIndex
--> 3Set the element at a particular index. Returns an updated array. If the index is out of range, the array is unaltered.
set 0 1302 (fromElement 42) --> fromElement 1302Push an element onto the end of an array.
Just (push 2 (fromElement 1)) --> fromList [1, 2]Append two arrays to a new one.
Just (append (repeat 2 42) (repeat 3 81)) --> fromList [42, 42, 81, 81, 81]The selected index of the resulting array will be the selected index of the first argument.
Get a sub-section of an array: (slice start end array). The start is a
zero-based index where we will start our slice. The end is a zero-based index
that indicates the end of the slice. The slice extracts up to but not including
end. If the resulting slice would be empty, Nothing is returned.
slice 0 3 (initialize 5 identity) --> fromList [0, 1, 2]
slice 1 4 (initialize 5 identity) --> fromList [1, 2, 3]
slice 2 2 (initialize 5 identity) --> NothingBoth the start and end indexes can be negative, indicating an offset from
the end of the array.
slice 1 -1 (initialize 5 identity) --> fromList [1, 2, 3]
slice -2 5 (initialize 5 identity) --> fromList [3, 4]This makes it pretty easy to pop the last element off of an array:
slice 0 -1 array
The selected index will be set to 0 for the resulting array.
Set the selected index for the given array. If the index is out of range, the array is unaltered (that is, the old selected index is kept).
initialize 5 identity
|> setSelectedIndex 2
|> getSelected
--> 2
initialize 5 identity
|> setSelectedIndex 7
|> getSelected
--> 0Converts the NonEmptyArray into a standard array.
import Array.Hamt as Array
fromArray (Array.fromList [1, 2])
|> Maybe.map toArray
--> Just (Array.fromList [1, 2])Create a list of elements from an array.
fromList [3, 5, 8]
|> Maybe.map toList
--> Just [3, 5, 8]Create an indexed list from an array. Each element of the array will be paired with its index.
fromList ["cat", "dog"]
|> Maybe.map toIndexedList
--> Just [(0, "cat"), (1, "dog")]Reduce an array from the left. Read foldl as fold from the left.
foldl (::) [] (initialize 3 identity) --> [2, 1, 0]Reduce an array from the right. Read foldr as fold from the right.
foldr (+) 0 (repeat 3 5) --> 15Keep only elements that satisfy the predicate. If no elements remain, Nothing is returned, otherwise Just the array of remaining elements.
isEven : Int -> Bool
isEven n =
n % 2 == 0
filter isEven (initialize 5 identity) --> fromList [0, 2, 4]The selected index will be set to 0 for the resulting array.
Apply a function to every element in an array.
map Basics.toString (repeat 5 1) --> repeat 5 "1"Apply a function to every element with its index as first argument.
Just (indexedMap (*) (repeat 5 2)) --> fromList [0, 2, 4, 6, 8]Return a representation of the NonEmptyArray as a string. The number in parantheses after the list of elements is the selected index.
import Array.NonEmpty as NEA
NEA.toString <| (initialize 3 identity) --> "NonEmptyArray [0,1,2] (0)"
An array that is known, at compile-time, to be non empty. It always has at least one element.