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 = Array

A collection of values with fast indexed reads.

Examples

fromList [1, 2, 3] : Array Int

Values

empty

empty : Array a

Return an empty array.

Examples

length empty == 0

isEmpty

isEmpty : Array a -> Bool

Determine if an array is empty.

Examples

isEmpty empty == True
isEmpty (fromList [1]) == False

length

length : Array a -> Int

Return the length of an array.

Examples

length (fromList [1, 2, 3]) == 3

initialize

initialize : Int -> (Int -> a) -> Array 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).

Examples

initialize 4 identity == fromList [0, 1, 2, 3]
initialize 4 (\n -> n * n) == fromList [0, 1, 4, 9]

repeat

repeat : Int -> a -> Array a

Create 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 a

Create an array from a List.

Examples

fromList [1, 2, 3] |> toList == [1, 2, 3]

get

get : Int -> Array a -> Maybe a

Return 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]) == Nothing

set

set : Int -> a -> Array a -> Array a

Set 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 a

Push an element onto the end.

Examples

push 3 (fromList [1, 2]) == fromList [1, 2, 3]

toList

toList : Array a -> List a

Create 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 -> b

Reduce an array from the right.

Examples

foldr (+) 0 (repeat 3 5) == 15

foldl

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

Reduce an array from the left.

Examples

foldl (::) [] (fromList [1, 2, 3]) == [3, 2, 1]

filter

filter : (a -> Bool) -> Array a -> Array a

Keep 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 b

Apply 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 b

Apply 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 a

Append 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 a

Get 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]