Array
Functions
Returns true
if any item in the array matches the predicate function
false
otherwise.
Array.any((number : Number) : Bool { number % 2 == 0 }, [1, 2, 3, 4]) == true
Array.any((number : Number) : Bool { number % 2 == 0 }, [1, 3]) == false
Puts two lists together:
Array.append([1,1,2] [3,5,8]) == [1,1,2,3,5,8]
Returns the element at the given index.
Array.at(0, [0]) == Maybe.just(0)
Array.at(1, [0]) == Maybe.nothing()
Flattens an Array(Maybe(a))
into an Array(a)
, by unwrapping the items
and skipping all elements of type Nothing
.
Array.compact([Maybe.just("A"), Maybe.nothing()]) == ["A"]
Concatenate a bunch of 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", ["a", "b", "c"]) == true
Array.contains("a", ["x", "y", "z"]) == false
Deletes every occurrence of the given element from the array.
Array.delete("a", ["a", "b", "c"]) == ["b", "c"]
Deletes the item of an array with the given index.
Drop n number of items from the left.
Array.drop(2, [1,2,3,4]) == [3,4]
Drop n number of items from the right.
Array.drop(2, [1,2,3,4]) == [1,2]
Finds the first element in the array that matches the predicate function.
Array.find((number : Number) : Bool { number % 2 == 0 }, [1, 2, 3, 4]) == Maybe.just(2)
Returns the first element of the array as Maybe.just(a)
or Maybe.nothing()
.
Array.first([]) == Maybe.nothing()
Array.first(["a", "x"]) == Maybe.just("a")
Returns the first element of the array or the default value.
Array.firstWithDefault("a", []) == "a"
Array.firstWithDefault("a", ["b", "x"]) == "b"
Map over a nested array and then flatten.
[[1,2],[1,5]]
|> Array.flatMap((a : Array(Number) : Array(Maybe(Number)) {
[Maybe.withDefault(Array.max(n), 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(2, [1,2,3,4,5,6,7]) == [[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(2, [1,2,3,4,5,6,7]) == [[1],[2,3],[4,5],[6,7]]
Returns the index of the item in the given array which matches the given value using the given function the generate the compared value.
Array.indexBy("a", (item : String) : String { item }, ["a","b","c"]) == 0
Returns the index of the given item in the given array.
Array.indexOf("a", ["a","b","c"]) == 1
Inserts the given item into the given position of the given array.
Array.insertAt("a", 0, ["b","c"]) == ["a","b","c"]
Inserts the given element between the elements of the given array.
Array.intersperse("a", ["x", "y", "z"]) == ["x", "a", "y", "a", "z"]
Returns whether or not the array is empty.
Array.isEmpty([]) == true
Array.isEmpty(["a", "b"]) == false
Returns the last element of the array as Just(a)
or Nothing
.
Array.last([]) == Maybe.nothing()
Array.last(["x", "a"]) == Maybe.just("a")
Returns the last element of the array or the default value.
Array.lastWithDefault("a", []) == "a"
Array.lastWithDefault("a", ["x", "b"]) == "b"
Creates a new array with the results of calling a provided function on every element in the given array.
Array.map((number : Number) : Number { number + 1 }, [1, 2, 3]) == [2, 3, 4]
Creates a new array with the results of calling a provided function on every element in the given array while providing the index of the element.
Array.mapWithIndex(
(number : Number, index : Number) : Number { number + index }, [1, 2, 3]) == [2, 4, 6]
Returns the maximum value of an array of numbers.
Array.max([0, 1, 2, 3, 4]) == Maybe.just(4)
Array.max([]) == Maybe.nothing()
Returns the minimum value of an array of numbers.
Array.min([0, 1, 2, 3, 4]) == Maybe.just(0)
Array.min([]) == Maybe.nothing()
Moves an item at the given 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(-1, 1, ["A", "B", "C"]) == ["A", "B", "C"] Array.move(10, 1, ["A", "B", "C"]) == ["A", "B", "C"] Array.move(0, 0, ["A", "B", "C"]) == ["A", "B", "C"]
If a negative number is supplied to to
then, the item is moved to the
first position.
Array.move(2, -1, ["A", "B", "C"]) == ["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(0, 10, ["A", "B", "C"]) == ["B", "C", "A"]
Push an element to the end of an array.
Array.push("a", []) == ["a"]
Array.push(4, [1, 2, 3]) == [1, 2, 3, 4]
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 given function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
Array.reduce(
0,
(memo : Number, item : Number) : Number { memo + item },
[1, 2, 3]) == 6
Reduce a list from the right.
[1,2,3,4,5]
|> Array.reduceRight(0, (acc : Number, n : Number) : Number { acc + n}) == 15
Returns all elements that do not matches the predicate function.
Array.reject((number : Number) : Bool { number % 2 == 0 }, [1, 2, 3, 4]) == [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]) == [4, 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((number : Number) : Bool { number % 2 == 0 }, [1, 2, 3, 4]) == [2, 4]
Sets the item at the given index to the given item of the given array.
Array.setAt(2, 5, [1,2,3]) == [1,2,5]
Returns the size of the array.
Array.size([]) == 0
Array.size([1, 2, 3]) == 3
Returns a copy of a portion of an array (end not included).
Array.slice(2, 4, ["ant", "bison", "camel", "duck", "elephant"]) == ["camel", "duck"]
Returns a new sorted array using the given sorting function.
Array.sort((a : Number, b : Number) : Number { a - b }, [4, 1, 3, 2]) == [1, 2, 3, 4]
Returns a new sorted array using the given functions return as the base of the sorting.
Array.sortBy((number : Number) : Number { number }, [4, 1, 3, 2]) == [1, 2, 3, 4]
Sums up the given array of numbers.
Array.sum([1, 2, 3]) == 6
Sums up the given array using the given function.
Array.sumBy((value : Number) : Number { value }, [1, 2, 3]) == 6
Swaps the items at the given indexes of the given array. It returns the array unchanged if there is no item at any of the given indexes.
Array.swap(0, 1, ["a","b"]) == ["b", "a"]
Take n number of items from the left.
Array.take(2, [1,2,3,4]) == [1,2]
Pushes a new item at the head of the array.
Array.unshift(2, [3,4]) == [2,3,4]
Updates the item at the given index of the given array using the given function.
Array.updateAt(
2, (number : Number) : Number {
number + 2
}, [0,1,2]) == [0,1,4]