Array
Functions
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
Merges two arrays together into a new one.
Array.append([1, 1, 2] [3, 5, 8]) == [1, 1, 2, 3, 5, 8]
Returns the element at the given index as a Maybe(item)
.
Array.at([0], 0) == Maybe::Just(0)
Array.at([0], 1) == Maybe::Nothing()
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"]
Concatenate multiple arrays into a single array.
Array.concat([[1,2],[3],[4,5]]) == [1,2,3,4,5]
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
Deletes every occurrence of the element from the array.
Array.delete(["a", "b", "c"], "a") == ["b", "c"]
Deletes the item of an array at the specified index.
Array.deleteAt(["a", "b", "c"], 1) == ["a", "c"]
Drop the specified number of items from the end of the array.
Array.dropEnd([1, 2, 3, 4], 2) == [1, 2]
Drop the specified number of items from the start of the array.
Array.dropStart([1, 2, 3, 4], 2) == [3, 4]
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)
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")
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
Returns the first element of the array or the default value.
Array.firstWithDefault(["b", "x"], "a") == "b"
Array.firstWithDefault([], "a") == "a"
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]
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],[7]]
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) == [[1],[2,3],[4,5],[6,7]]
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
Returns the index of the specified item in the array.
Array.indexOf(["a","b","c"], "a") == 0
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"]
Inserts the element between the elements of the array.
Array.intersperse(["x", "y", "z"], "a") == ["x", "a", "y", "a", "z"]
Returns whether or not the array is empty.
Array.isEmpty(["a", "b"]) == false
Array.isEmpty([]) == true
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
Returns the last element of the array or the default value.
Array.lastWithDefault(["x", "b"], "a") == "b"
Array.lastWithDefault([], "a") == "a"
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]
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]
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
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
Moves an item at the index from
to a new index to
.
The array is returned as is if:
-
from
andto
are the same -
a negative number is supplied to
from
-
a number is supplied to
from
which is grater the the length of the arrayArray.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 an element to the end of an array.
Array.push([1, 2, 3], 4) == [1, 2, 3, 4]
Array.push([], "a") == ["a"]
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]
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
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
Returns all elements that do not match the predicate function.
Array.reject([1, 2, 3, 4], (number : Number) : Bool { number % 2 == 0 }) == [1, 3]
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]
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]
Returns a random element from the array.
Array.sample(["a"]) == Maybe::Just("a")
Array.sample() == Maybe::Nothing()
Returns all elements that match the predicate function.
Array.select([1, 2, 3, 4], (number : Number) : Bool { number % 2 == 0 }) == [2, 4]
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]
Returns the size of the array.
Array.size([1, 2, 3]) == 3
Array.size([]) == 0
Returns a copy of a portion of an array (end not included).
Array.slice(["ant", "bison", "camel", "duck", "elephant"], 2, 4) == ["camel", "duck"]
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 bArray.sort([4, 1, 3, 2], (a : Number, b : Number) : Number { a - b }) == [1, 2, 3, 4]
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]
Sums up the array of numbers.
Array.sum([1, 2, 3]) == 6
Sums up the array using the specified function.
Array.sumBy([1, 2, 3], (value : Number) : Number { value }) == 6
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"]
Takes the specified number of items from the end of the array.
Array.takeEnd([1, 2, 3, 4], 2) == [3, 4]
Takes the specified number of items from the start of the array.
Array.takeStart([1, 2, 3, 4], 2) == [1, 2]
Removes duplicate items from the array.
Array.uniq(["a", "a", "b", "b", "c"] == ["a", "b", "c"]
Pushes a new item at the head of the array.
Array.unshift([3, 4], 2) == [2, 3, 4]
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]