An array that always contains at least one element.
Creates a new, non empty array with a single element.
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]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).
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.
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.
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) --> 0Set 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]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
Converts 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]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.
import Array.NonEmpty as NEA
NEA.toString <| (initialize 3 identity) --> "NonEmptyArray [0,1,2]"
An array that is known, at compile-time, to be non empty. It always has at least one element.