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 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 an array of numbers, every element increasing by one. You give the lowest and highest number that should be in the array.
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.
Reduce the array from the left. The reducing function is passed the index of the current value.
indexedFoldl (\idx val sum -> idx + val + sum) 0 [ 1, 2, 3 ] == 9
Reduce the array from the right. The reducing function
is passed the index of the current value. Same as indexedFoldl
but the execution order is reversed.
Keep values that pass the test.
keepIf (\n -> n < 3) [ 1, 2, 3, 4 ] == [ 1, 2 ]
Same as keepIf
but the test is also applied to the index of each element.
indexedKeepIf (\idx val -> idx + val > 4) [ 1, 2, 3, 4 ] == [ 3, 4 ]
Remove unwanted results of a map operation.
mapAndKeepJust String.toInt [ "3", "not a number", "-5" ] == [ 3, -5 ]
mapAndKeepJust 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.
A negative index looks up an element in reverse from the end of the array.
get 1 [ 1, 2, 3 ] == Just 2
get 10 [ 1, 2, 3 ] == Nothing
get -1 [ 1, 2, 3 ] == Just 3
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
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 array.
minimum [3,2,1] == Just 1
minimum [] == Nothing
Find the maximum element in a non-empty array.
maximum [3,2,1] == Just 3
maximum [] == Nothing
Modify
Replace the element at the given index, or return the array unmodified if the index is out of bounds. Passing a negative index means you want to replace an element counting backwards from the end of the array.
set 1 10 [ 1, 2, 3 ] == [ 1, 10, 3 ]
set 10 10 [ 1, 2, 3 ] == [ 1, 2, 3 ]
set -1 10 [ 1, 2, 3 ] == [ 1, 2, 10 ]
This function works just like set except it takes in an array of values to set, allowing you to replace several values at once.
If the provided index is out of bounds, elements will be added at the beginning (negative index) or at the end (positive index).
setMany 1 [ 0, 0 ] [ 1, 2, 3, 4 ] == [ 1, 0, 0, 4 ]
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 ]
Insert a new value into the array at the given index. The value already at the given index, as well as all subsequent values, will be moved one space to the right.
Passing a negative index means you want to replace an element counting backwards from the end of the array.
If the provided index is out of bounds, the element will be added at the beginning (negative index) or at the end (positive index).
insert 1 0 [ 1, 2, 3 ] == [ 1, 0, 2, 3 ]
This function works just like insert but allows you to insert multiple values at once.
insertMany 1 [ 0, 0 ] [ 1, 2, 3 ] == [ 1, 0, 0, 2, 3 ]
Remove an element from an array.
Passing a negative index means you want to replace an element counting backwards from the end of the array.
If the index is out of bounds, no element will be removed.
remove 1 [ 1, 2, 3 ] == [ 1, 3 ]
This function works just like remove, except it allows you to remove multiple elements at once.
The first argument is the index from where to remove elements from, the second argument is the number of elements to remove.
remove 0 2 [ 1, 2, 3 ] == [ 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 ]
Allows you to perform multiple modifications in a single operation. Splice takes an index as its first argument. This marks the point where modifications will be performed. The second argument is the number of elements to remove. The third argument is the elements that will be inserted. The argument order represents the order of modifications. Elements will be removed before new elements are inserted.
Passing a negative index means you want to replace an element counting backwards from the end of the array.
If the index is out of bounds no elements will be removed, but elements will be added at the beginning (negative index) or at the end (positive index).
splice 2 0 [ 0 ] [ 1, 2, 3 ] == [ 1, 2, 0, 3 ]
splice 2 1 [ 0 ] [ 1, 2, 3 ] == [ 1, 2, 0 ]
splice 2 1 [] [ 1, 2, 3 ] == [ 1, 2 ]
Combine
Combine two arrays so that the first array becomes the prefix, and the second array becomes the postfix of the resulting array.
prepend [ 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.
append [ 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.
mapAndFlatten f xs == flatten (map f xs)
Places the given value between all members of the given array.
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.
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 ]
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 ]
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 ] }
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 =
when compare a b is
LT -> GT
EQ -> EQ
GT -> LT
This is also the most general sort function, allowing you to define any other: sort == sortWith compare