Array
You can create an Array
using the [1, 2, 3]
syntax. This module has a bunch of
functions to help you work with them.
An Array is an ordered collection of elements.
Create
An empty array.
Create an array containing a single value.
Create an array of n
elements, containing the elements
resulting from calling fn
with offset + index
.
initialize 3 5 identity == [ 5, 6, 7 ]
In the above example, we create an array containing 3 integers starting at 5.
Create an array with n
copies of a value:
repeat 5 3 == [ 3, 3, 3, 3, 3 ]
Create a list of numbers, every element increasing by one. You give the lowest and highest number that should be in the list.
range 3 6 == [3, 4, 5, 6]
range 3 3 == [3]
range 6 3 == []
Transform
Apply a function on every element in an array.
map negate [ 1, 4, 9 ] == [ -1, -4, -9 ]
So map func [ a, b, c ]
is the same as [ func a, func b, func c ]
Same as map
but the function is also applied to the index of each element.
indexedMap (\idx val -> [idx, val]) [ 3, 3, 3 ] == [ [ 0, 3 ], [ 1, 3 ], [ 2, 3 ] ]
Reduce the array from the left.
foldl (+) 0 [ 1, 2, 3 ] == 6
So `foldl step state [ 1, 2, 3 ] is like saying:
state
|> step 1
|> step 2
|> step 3
Reduce the array from the right. Same as foldl
but
the execution order is reversed.
Keep values that pass the test.
filter (\n -> n < 3) [ 1, 2, 3, 4 ] == [ 1, 2 ]
Filter out unwanted results of a map operation.
filterMap String.toInt [ "3", "not a number", "-5" ] == [ 3, -5 ]
filterMap identity [ Just 1, Nothing ] == [ 1 ]
Reverse an array.
reverse [ 1, 2, 3 ] == [ 3, 2, 1 ]
Query
Check if an array is empty.
isEmpty [] == True
isEmpty [ 1, 2, 3 ] == False
Return the length of an array.
length [ 1, 2, 3 ] == 3
Retrieve the element at a given index, or Nothing
if the index is out of bounds.
get 1 [ 1, 2, 3 ] == Just 2
get 10 [ 1, 2, 3 ] == Nothing
Find the first value that passes the test.
find (\n -> n > 0) [ -1, 0, 1, 2 ] == Just 1
Find the last value that passes the test.
find (\n -> n > 0) [ -1, 0, 1, 2 ] == Just 2
Figure out whether an array contains a value.
member 9 [1,2,3,4] == False
member 4 [1,2,3,4] == True
Determine if any elements pass the test.
any isEven [2,3] == True
any isEven [1,3] == False
any isEven [] == False
Determine if all elements pass the test.
all isEven [2,4] == True
all isEven [2,3] == False
all isEven [] == True
Find the minimum element in a non-empty list.
minimum [3,2,1] == Just 1
minimum [] == Nothing
Find the maximum element in a non-empty list.
maximum [3,2,1] == Just 3
maximum [] == Nothing
Modify
Replaces the element at the given index, or returns the array unmodified if the index is out of bounds.
set 1 10 [ 1, 2, 3 ] == [ 1, 10, 3 ]
set 10 10 [ 1, 2, 3 ] == [ 1, 2, 3 ]
Update a value at the given index using a function. If the index is out of bounds, nothing happens.
update 1 (\n -> n + 1) [ 1, 2, 3 ] == [ 1, 3, 3 ]
update 10 (\n -> n + 1) [ 1, 2, 3 ] == [ 1, 2, 3 ]
Add a value to the start of the array.
pushFirst 1 [] == [ 1 ]
pushFirst 5 [ 1, 4, 9 ] == [ 5, 1, 4, 9 ]
Add a value to the end of the array.
pushLast 1 [] == [ 1 ]
pushLast 5 [ 1, 4, 9 ] == [ 1, 4, 9, 5 ]
Combine
Combine two arrays so that the first array becomes the prefix, and the second array becomes the postfix of the resulting array.
prefix [ 1, 2, 3 ] [ 4, 5, 6 ] == [ 1, 2, 3, 4, 5, 6 ]
You can also use the ++
operator for this purpose.
[ 1, 2, 3 ] ++ [ 4, 5, 6 ] == [ 1, 2, 3, 4, 5, 6 ]
Combine two arrays so that the first array becomes the postfix, and the second array becomes the prefix of the resulting array.
postfix [ 1, 2, 3 ] [ 4, 5, 6 ] == [ 4, 5, 6, 1, 2, 3 ]
Combine a bunch of arrays into a single array.
flatten [ [ 1 ], [ 2 ], [ 4, 5 ] ] == [ 1, 2, 4, 5 ]
Map a given function onto an array, then flatten the resulting array.
flatMap f xs == flatten (map f xs)
Places the given value between all members of the given list.
intersperse "on" [ "turtles", "turtles", "turtles"] == [ "turtles", "on", "turtles", "on", "turtles"]
Combine two arrays, combining them with the given function. If one array is longer, the extra elements are dropped.
map2 (\x y -> { x = x, y = y }) [ 1 ] [ 2 ] == [ { x = 1, y = 2 } ]
Combine three arrays, combining them with the given function. If one array is longer, the extra elements are dropped.
map3 (\x y z -> { x = x, y = y, z = z }) [ 1 ] [ 2 ] [ 3 ] == [ { x = 1, y = 2, z = 3 } ]
Deconstruct
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 no including, the end
.
Both start
and end
can be negative, indicating an offset from the end
of the array. Removing the last element of the array can be expressed as:
`slice 0 -1 arr`.
In the case of an impossible slice, the empty array is returned.
Remove the first n
elements of the array.
dropFirst 5 [ 1 ] == []
dropFirst 1 [ 1, 2, 3 ] == [ 2, 3 ]
Remove the last n
elements of the array.
dropLast 1 [ 1, 2, 3 ] == [ 1, 2 ]
Take the first n
elements from the array.
takeFirst 2 [ 1, 2, 3 ] == [ 1, 2 ]
Take the last n
elements from the array.
takeLast 2 [ 1, 2, 3 ] == [ 2, 3 ]
Split an array into its first element, and its remaining elements, if possible.
popFirst [ 1, 2, 3 ] == Just { first = 1, rest = [ 2, 3 ] }
Split an array into its last element, and its remaining elements, if possible.
popFirst [ 1, 2, 3 ] == Just { last = 3, initial = [ 1, 2 ] }
Retrieve the first element of the array, if it exists.
first [ 1, 2, 3 ] == Just 1
Retrieve the last element of the array, if it exists.
last [ 1, 2, 3 ] == Just 3
Divide elements into two arrays based on the result of a boolean test.
partition (\x -> x < 3) [ 0, 1, 2, 3, 4, 5 ] == { trues = [ 0, 1, 2 ], falses = [ 3, 4, 5 ] }
Sort
Sort values from lowest to highest
sort [ 3, 1, 5 ] == [ 1, 3, 5 ]
Sort values by a derived property.
sortBy String.length [ "mouse", "cat" ] == [ "cat", "mouse" ]
Sort values with a custom comparison function.
sortWith flippedComparison [1,2,3,4,5] == [5,4,3,2,1]
flippedComparison a b =
case compare a b of
LT -> GT
EQ -> EQ
GT -> LT
This is also the most general sort function, allowing you to define any other: sort == sortWith compare