basti1302/elm-non-empty-array/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Array.NonEmpty

An array that always contains at least one element.

Non Empty Array

An array that is known, at compile-time, to be non empty. It always has at least one element.

Creation

fromElement : a -> NonEmptyArray a

Creates a new, non empty array with a single element.

initialize : Int -> (Int -> a) -> NonEmptyArray a

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]
repeat : Int -> a -> NonEmptyArray a

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).

fromList : List a -> Maybe (NonEmptyArray a)

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.

fromArray : Array a -> Maybe (NonEmptyArray a)

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.

Query

length : NonEmptyArray a -> Int

Return the length of the array.

get : Int -> NonEmptyArray a -> Maybe a

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) -->  Nothing

Notice 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.

getFirst : NonEmptyArray a -> a

Return the first element of a NonEmptyArray.

getFirst (initialize 3 identity) --> 0

Manipulate

set : Int -> a -> NonEmptyArray a -> NonEmptyArray a

Set 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 1302
push : a -> NonEmptyArray a -> NonEmptyArray a

Push an element onto the end of an array.

Just (push 2 (fromElement 1)) --> fromList [1, 2]
append : NonEmptyArray a -> NonEmptyArray a -> NonEmptyArray a

Append two arrays to a new one.

Just (append (repeat 2 42) (repeat 3 81)) --> fromList [42, 42, 81, 81, 81]
slice : Int -> Int -> NonEmptyArray a -> Maybe (NonEmptyArray a)

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) --> Nothing

Both 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

Conversions

toArray : NonEmptyArray a -> Array a

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])
toList : NonEmptyArray a -> List a

Create a list of elements from an array.

fromList [3, 5, 8]
    |> Maybe.map toList
--> Just [3, 5, 8]
toIndexedList : NonEmptyArray a -> List ( Int, a )

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")]

Transform

foldl : (a -> b -> b) -> b -> NonEmptyArray a -> b

Reduce an array from the left. Read foldl as fold from the left.

foldl (::) [] (initialize 3 identity) --> [2, 1, 0]
foldr : (a -> b -> b) -> b -> NonEmptyArray a -> b

Reduce an array from the right. Read foldr as fold from the right.

foldr (+) 0 (repeat 3 5) --> 15
filter : (a -> Bool) -> NonEmptyArray a -> Maybe (NonEmptyArray a)

Keep 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]
map : (a -> b) -> NonEmptyArray a -> NonEmptyArray b

Apply a function to every element in an array.

map Basics.toString (repeat 5 1) --> repeat 5 "1"
indexedMap : (Int -> a -> b) -> NonEmptyArray a -> NonEmptyArray b

Apply a function to every element with its index as first argument.

Just (indexedMap (*) (repeat 5 2)) --> fromList [0, 2, 4, 6, 8]

Display

toString : NonEmptyArray a -> String

Return a representation of the NonEmptyArray as a string.

import Array.NonEmpty as NEA


NEA.toString <| (initialize 3 identity) --> "NonEmptyArray [0,1,2]"