Pages
Standard Library

Search
Entities

# Array

## Functions

any
(
array
:
Array(item)
function
:
Function(item, Bool)
)
:
Bool

Returns `true` if any item in the array matches the predicate function `false` otherwise.

``````Array.any([1, 2, 3, 4], (number : Number) : Bool { number % 2 == 0 }) == true
Array.any([1, 3], (number : Number) : Bool { number % 2 == 0 }) == false
``````
append
(
array1
:
Array(item)
array2
:
Array(item)
)
:
Array(item)

Merges two arrays together into a new one.

``````Array.append([1, 1, 2] [3, 5, 8]) == [1, 1, 2, 3, 5, 8]
``````
at
(
array
:
Array(item)
index
:
Number
)
:
Maybe(item)

Returns the element at the given index as a `Maybe(item)`.

``````Array.at(, 0) == Maybe::Just(0)
Array.at(, 1) == Maybe::Nothing()
``````
compact
(
array
:
Array(Maybe(item))
)
:
Array(item)

Flattens an `Array(Maybe(item))` into an `Array(item)`, by unwrapping the items and skipping all elements of `Maybe::Nothing`.

``````Array.compact([Maybe::Just("A"), Maybe::Nothing()]) == ["A"]
``````
concat
(
arrays
:
Array(Array(item))
)
:
Array(item)

Concatenate multiple arrays into a single array.

``````Array.concat([[1,2],,[4,5]]) == [1,2,3,4,5]
``````
contains
(
array
:
Array(item)
other
:
item
)
:
Bool

Checks whether or not the given element exists in the array.

``````Array.contains(["a", "b", "c"], "a") == true
Array.contains(["x", "y", "z"], "a") == false
``````
delete
(
array
:
Array(item)
what
:
item
)
:
Array(item)

Deletes every occurrence of the element from the array.

``````Array.delete(["a", "b", "c"], "a") == ["b", "c"]
``````
deleteAt
(
array
:
Array(item)
index
:
Number
)
:
Array(item)

Deletes the item of an array at the specified index.

``````Array.deleteAt(["a", "b", "c"], 1) == ["a", "c"]
``````
dropEnd
(
array
:
Array(item)
number
:
Number
)
:
Array(item)

Drop the specified number of items from the end of the array.

``````Array.dropEnd([1, 2, 3, 4], 2) == [1, 2]
``````
dropStart
(
array
:
Array(item)
number
:
Number
)
:
Array(item)

Drop the specified number of items from the start of the array.

``````Array.dropStart([1, 2, 3, 4], 2) == [3, 4]
``````
find
(
array
:
Array(item)
function
:
Function(item, Bool)
)
:
Maybe(item)

Finds the first element in the array that matches the predicate function.

``````Array.find([1, 2, 3, 4], (number : Number) { number % 2 == 0 }) == Maybe::Just(2)
``````
findByAndMap
(
array
:
Array(item)
function
:
Function(item, Tuple(Bool, result))
)
:
Maybe(b)

Finds the first element in the array that matches the predicate function and returns the second item in the resulting tuple.

``````Array.findByAndMap(
[1, 2, 3, 4],
(number : Number) : (Bool, value) { {number % 2 == 0, "Two"} }
) == Maybe::Just("Two")
``````
first
(
array
:
Array(item)
)
:
Maybe(item)

Returns the first element of the array as `Maybe::Just(item)` or `Maybe::Nothing`.

``````Array.first(["a", "x"]) == Maybe::Just("a")
Array.first([]) == Maybe::Nothing
``````
firstWithDefault
(
array
:
Array(item)
item
:
item
)
:
item

Returns the first element of the array or the default value.

``````Array.firstWithDefault(["b", "x"], "a") == "b"
Array.firstWithDefault([], "a") == "a"
``````
flatMap
(
array
:
Array(Array(item))
function
:
Function(Array(item), Array(result))
)
:
Array(result)

Map over a nested array and then flatten.

``````Array.flatMap(
[[1,2],[1,5]],
(item : Array(Number) : Array(Maybe(Number)) {
[Maybe.withDefault(Array.max(item), 0)]
}) == [2,5]
``````
groupsOf
(
array
:
Array(item)
size
:
Number
)
:
Array(Array(item))

Group an array into sub groups of specified length (all items are included so the last group maybe shorter if after grouping there is a remainder)

``````Array.groupsOf([1,2,3,4,5,6,7], 2) == [[1,2],[3,4],[5,6],]
``````
groupsOfFromEnd
(
array
:
Array(item)
size
:
Number
)
:
Array(Array(item))

Group an array into sub groups of specified length (all items are included so the last group maybe shorter if after grouping there is a remainder) starting from the end of the array.

``````Array.groupsOfFromEnd([1,2,3,4,5,6,7], 2) == [,[2,3],[4,5],[6,7]]
``````
indexBy
(
array
:
Array(item)
value
:
result
method
:
Function(item, result)
)
:
Number

Returns the index of the item in the array which matches the value using the function to generate the compared value.

``````Array.indexBy(["a","b","c"], "a", (item : String) : String { item }) == 0
``````
indexOf
(
array
:
Array(item)
search
:
item
)
:
Maybe(Number)

Returns the index of the specified item in the array.

``````Array.indexOf(["a","b","c"], "a") == 0
``````
insertAt
(
array
:
Array(item)
item
:
item
position
:
Number
)
:
Array(item)

Inserts the item into the specified position of the array, pushing items toward the end of the array. If the length is negative the item will be inserted at the start of the array.

``````Array.insertAt(["b","c"], "a", 0) == ["a","b","c"]
``````
intersperse
(
array
:
Array(item)
item
:
item
)
:
Array(item)

Inserts the element between the elements of the array.

``````Array.intersperse(["x", "y", "z"], "a") == ["x", "a", "y", "a", "z"]
``````
isEmpty
(
array
:
Array(item)
)
:
Bool

Returns whether or not the array is empty.

``````Array.isEmpty(["a", "b"]) == false
Array.isEmpty([]) == true
``````
last
(
array
:
Array(item)
)
:
Maybe(item)

Returns the last element of the array as `Maybe::Just(a)` or `Maybe::Nothing`.

``````Array.last(["x", "a"]) == Maybe::Just("a")
Array.last([]) == Maybe::Nothing
``````
lastWithDefault
(
array
:
Array(item)
item
:
item
)
:
item

Returns the last element of the array or the default value.

``````Array.lastWithDefault(["x", "b"], "a") == "b"
Array.lastWithDefault([], "a") == "a"
``````
map
(
array
:
Array(item)
method
:
Function(item, result)
)
:
Array(result)

Creates a new array with the results of calling a provided function on every element in the array.

``````Array.map([1, 2, 3], (number : Number) : Number { number + 1 }) == [2, 3, 4]
``````
mapWithIndex
(
array
:
Array(item)
method
:
Function(item, Number, result)
)
:
Array(result)

Creates a new array with the results of calling a provided function on every element in the array while providing the index of the element.

``````Array.mapWithIndex(
[1, 2, 3],
(number : Number, index : Number) : Number { number + index }
) == [2, 4, 6]
``````
max
(
array
:
Array(Number)
)
:
Maybe(Number)

Returns the maximum value of an array of numbers. It's a maybe because the array might not have items in it.

``````Array.max([0, 1, 2, 3, 4]) == Maybe::Just(4)
Array.max([]) == Maybe::Nothing
``````
min
(
array
:
Array(Number)
)
:
Maybe(Number)

Returns the minimum value of an array of numbers. It's a maybe because the array might not have items in it.

``````Array.min([0, 1, 2, 3, 4]) == Maybe::Just(0)
Array.min([]) == Maybe::Nothing
``````
move
(
array
:
Array(item)
from
:
Number
to
:
Number
)
:
Array(item)

Moves an item at the index `from` to a new index `to`.

The array is returned as is if:

• `from` and `to` are the same

• a negative number is supplied to `from`

• a number is supplied to `from` which is grater the the length of the array

Array.move(["A", "B", "C"], -1, 1) == ["A", "B", "C"] Array.move(["A", "B", "C"], 10, 1) == ["A", "B", "C"] Array.move(["A", "B", "C"], 0, 0) == ["A", "B", "C"]

If a negative number is supplied to `to` then, the item is moved to the first position.

``````Array.move(["A", "B", "C"], 2, -1) == ["C", "A", "B"]
``````

If a number is supplied to `to` which is grater the the length of the array, then the item is moved to the last position.

``````Array.move(["A", "B", "C"], 0, 10) == ["B", "C", "A"]
``````
push
(
array
:
Array(item)
item
:
item
)
:
Array(item)

Push an element to the end of an array.

``````Array.push([1, 2, 3], 4) == [1, 2, 3, 4]
Array.push([], "a") == ["a"]
``````
range
(
from
:
Number
to
:
Number
)
:
Array(Number)

Creates an array of numbers starting from the first argument and ending in the last.

``````Array.range(0, 5) == [0, 1, 2, 3, 4, 5]
``````
reduce
(
array
:
Array(item)
initial
:
memo
function
:
Function(memo, item, memo)
)
:
memo

Applies the function against an accumulator and each element in the array (from start to end) to reduce it to a single value.

``````Array.reduce(
[1, 2, 3],
0,
(memo : Number, item : Number) : Number { memo + item }) == 6
``````
reduceEnd
(
array
:
Array(item)
initial
:
memo
function
:
Function(memo, item, memo)
)
:
memo

Applies the function against an accumulator and each element in the array (from end to start) to reduce it to a single value.

``````Array.reduceEnd(
[1,2,3,4,5],
0,
(acc : Number, n : Number) : Number { acc + n}) == 15
``````
reject
(
array
:
Array(item)
function
:
Function(item, Bool)
)
:
Array(item)

Returns all elements that do not match the predicate function.

``````Array.reject([1, 2, 3, 4], (number : Number) : Bool { number % 2 == 0 }) == [1, 3]
``````
reverse
(
array
:
Array(item)
)
:
Array(item)

Returns a new array where the elements are reversed. The first array element becomes the last, and the last array element becomes the first.

``````Array.reverse([1, 2, 3]) == [3, 2, 1]
``````
reverseIf
(
array
:
Array(item)
condition
:
Bool
)
:
Array(item)

Returns a new array where the elements are reversed if the condition is true.

``````Array.reverseIf([1, 2, 3], false) == [1, 2, 3]
Array.reverseIf([1, 2, 3], true) == [3, 2, 1]
``````
sample
(
array
:
Array(item)
)
:
Maybe(item)

Returns a random element from the array.

``````Array.sample(["a"]) == Maybe::Just("a")
Array.sample() == Maybe::Nothing()
``````
select
(
array
:
Array(item)
function
:
Function(item, Bool)
)
:
Array(item)

Returns all elements that match the predicate function.

``````Array.select([1, 2, 3, 4], (number : Number) : Bool { number % 2 == 0 }) == [2, 4]
``````
setAt
(
array
:
Array(item)
index
:
Number
item
:
item
)
:
Array(item)

Sets the item at index to the item of the array, if the specified index is not found in the array it returns the array unchanged.

``````Array.setAt([1,2,3], 2, 5) == [1,2,5]
``````
size
(
array
:
Array(item)
)
:
Number

Returns the size of the array.

``````Array.size([1, 2, 3]) == 3
Array.size([]) == 0
``````
slice
(
array
:
Array(item)
begin
:
Number
end
:
Number
)
:
Array(item)

Returns a copy of a portion of an array (end not included).

``````Array.slice(["ant", "bison", "camel", "duck", "elephant"], 2, 4) == ["camel", "duck"]
``````
sort
(
array
:
Array(item)
function
:
Function(item, item, Number)
)
:
Array(item)

Returns a new sorted array using the sorting function `compareFunction(a, b)`. Items are sorted using a number:

• `> 0` - sort b before a

• `< 0` - sort a before b

• `0` - keep original order of a and b

Array.sort([4, 1, 3, 2], (a : Number, b : Number) : Number { a - b }) == [1, 2, 3, 4]

sortBy
(
array
:
Array(item)
function
:
Function(item, result)
)
:
Array(item)

Returns a new sorted array using the functions return as the base of the sorting.

``````Array.sortBy([4, 1, 3, 2], (number : Number) : Number { number }) == [1, 2, 3, 4]
``````
sum
(
array
:
Array(Number)
)
:
Number

Sums up the array of numbers.

``````Array.sum([1, 2, 3]) == 6
``````
sumBy
(
array
:
Array(item)
method
:
Function(item, Number)
)
:
Number

Sums up the array using the specified function.

``````Array.sumBy([1, 2, 3], (value : Number) : Number { value }) == 6
``````
swap
(
array
:
Array(item)
index1
:
Number
index2
:
Number
)
:
Array(item)

Swaps the items at the specified indexes of the array. It returns the array unchanged if there is no item at any of the specified indexes.

``````Array.swap(["a","b"], 0, 1) == ["b", "a"]
``````
takeEnd
(
array
:
Array(item)
number
:
Number
)
:
Array(item)

Takes the specified number of items from the end of the array.

``````Array.takeEnd([1, 2, 3, 4], 2) == [3, 4]
``````
takeStart
(
array
:
Array(item)
number
:
Number
)
:
Array(item)

Takes the specified number of items from the start of the array.

``````Array.takeStart([1, 2, 3, 4], 2) == [1, 2]
``````
uniq
(
array
:
Array(item)
)
:
Array(item)

Removes duplicate items from the array.

``````Array.uniq(["a", "a", "b", "b", "c"] == ["a", "b", "c"]
``````
unshift
(
array
:
Array(item)
item
:
item
)
:
Array(item)

Pushes a new item at the head of the array.

``````Array.unshift([3, 4], 2) == [2, 3, 4]
``````
updateAt
(
array
:
Array(item)
index
:
Number
method
:
Function(item, item)
)
:
Array(item)

Updates the item at the given index of the given array using the given function.

``````Array.updateAt(
[0,1,2], 2, (number : Number) : Number {
number + 2
}) == [0,1,4]
``````