Array
Core
Fast immutable arrays. On the BEAM this is backed by Erlang :array (persistent tree; ~O(log n) indexed access).
Types
Array
type Array a = ArrayA collection of values with fast indexed reads.
Examples
fromList [1, 2, 3] : Array IntValues
empty
empty : Array aReturn an empty array.
Examples
length empty == 0isEmpty
isEmpty : Array a -> BoolDetermine if an array is empty.
Examples
isEmpty empty == True
isEmpty (fromList [1]) == Falselength
length : Array a -> IntReturn the length of an array.
Examples
length (fromList [1, 2, 3]) == 3initialize
initialize : Int -> (Int -> a) -> Array aInitialize an array. initialize n f creates an array of length n with the element at index i initialized to the result of (f i).
Examples
initialize 4 identity == fromList [0, 1, 2, 3]
initialize 4 (\n -> n * n) == fromList [0, 1, 4, 9]repeat
repeat : Int -> a -> Array aCreate an array with a given length, filled with a default element.
Examples
repeat 5 0 == fromList [0, 0, 0, 0, 0]
repeat 3 "cat" == fromList ["cat", "cat", "cat"]fromList
fromList : List a -> Array aCreate an array from a List.
Examples
fromList [1, 2, 3] |> toList == [1, 2, 3]get
get : Int -> Array a -> Maybe aReturn Just the element at the index, or Nothing if out of range.
Examples
get 0 (fromList [0, 1, 2]) == Just 0
get 5 (fromList [0, 1, 2]) == Nothingset
set : Int -> a -> Array a -> Array aSet the element at a particular index. Returns the array unchanged if the index is out of range.
Examples
set 1 7 (fromList [1, 2, 3]) == fromList [1, 7, 3]push
push : a -> Array a -> Array aPush an element onto the end.
Examples
push 3 (fromList [1, 2]) == fromList [1, 2, 3]toList
toList : Array a -> List aCreate a list of elements from an array, from first to last.
Examples
toList (fromList [3, 5, 8]) == [3, 5, 8]toIndexedList
toIndexedList : Array a -> List (Int, a)Create an indexed list from an array. Each element of the array will be paired with its index.
Examples
toIndexedList (fromList ["cat", "dog"]) == [(0, "cat"), (1, "dog")]foldr
foldr : (a -> b -> b) -> b -> Array a -> bReduce an array from the right.
Examples
foldr (+) 0 (repeat 3 5) == 15foldl
foldl : (a -> b -> b) -> b -> Array a -> bReduce an array from the left.
Examples
foldl (::) [] (fromList [1, 2, 3]) == [3, 2, 1]filter
filter : (a -> Bool) -> Array a -> Array aKeep elements that satisfy the test.
Examples
filter (\n -> remainderBy 2 n == 0) (fromList [1, 2, 3, 4]) == fromList [2, 4]map
map : (a -> b) -> Array a -> Array bApply a function to every element.
Examples
map (\n -> n * n) (fromList [1, 2, 3]) == fromList [1, 4, 9]indexedMap
indexedMap : (Int -> a -> b) -> Array a -> Array bApply a function to every element with its index.
Examples
indexedMap (*) (fromList [5, 5, 5]) == fromList [0, 5, 10]append
append : Array a -> Array a -> Array aAppend two arrays to create a new one.
Examples
append (repeat 2 42) (repeat 3 81) == fromList [42, 42, 81, 81, 81]slice
slice : Int -> Int -> Array a -> Array aGet a sub-section of an array: slice start end array. Indexes can be negative, counting from the end.
Examples
slice 0 3 (fromList [0, 1, 2, 3, 4]) == fromList [0, 1, 2]
slice 1 -1 (fromList [0, 1, 2, 3, 4]) == fromList [1, 2, 3]