Try Install Learn Blog Api Packages Github
Pages

Array

Module for functions to manipulate immutable arrays.

Functions

any
(
func
:
Function(a, Bool)
array
:
Array(a)
)
:
Bool

Returns true if any item in the array matches the prdicate 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
append
(
array1
:
Array(a)
array2
:
Array(a)
)
:
Array(a)

Put two lists together:

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

Returns the element at the given index.

Array.at(0, [0]) == Maybe.just(0)
Array.at(1, [0]) == Maybe.nothing()
compact
(
array
:
Array(Maybe(a))
)
:
Array(a)

Flattens an Array(Maybe(a)) into an Array(a), by unwrapping the items and skipping nothings.

Array.compact([Maybe.just("A"), Maybe.nothing()]) == ["A"]
concat
(
arrays
:
Array(Array(a))
)
:
Array(a)

Concatenate a bunch of arrays into a single array:

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

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
delete
(
what
:
a
array
:
Array(a)
)
:
Array(a)

Deletes every occurence of the given element from the array.

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

Deletes the item of an array with the given index.

drop
(
number
:
Number
array
:
Array(a)
)
:
Array(a)

Drop n number of items from the left.

Array.drop(2, [1,2,3,4]) == [3,4]
dropRight
(
number
:
Number
array
:
Array(a)
)
:
Array(a)

Drop n number of items from the right.

Array.drop(2, [1,2,3,4]) == [1,2]
find
(
func
:
Function(a, Bool)
array
:
Array(a)
)
:
Maybe(a)

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)
first
(
array
:
Array(a)
)
:
Maybe(a)

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")
firstWithDefault
(
item
:
a
array
:
Array(a)
)
:
a

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

Array.firstWithDefault("a", []) == "a"
Array.firstWithDefault("a", ["b", "x"]) == "b"
flatMap
(
func
:
Function(a, Array(b))
array
:
Array(a)
)
:
Array(b)

Map over a nested array and then flatten.

[[1,2],[1,5]]
|> Array.flatMap((a : Array(Number) : Array(Number) {
  [Array.max(n)]
}) == [2,5]
groupsOf
(
size
:
Number
array
:
Array(a)
)
:
Array(Array(a))

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]]
groupsOfFromEnd
(
size
:
Number
array
:
Array(a)
)
:
Array(Array(a))

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. 7 Array.groupsOfFromEnd(2, [1,2,3,4,5,6,7]) == [[1],[2,3],[4,5],[6,7]]

indexBy
(
value
:
b
method
:
Function(a, b)
array
:
Array(a)
)
:
Number

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
indexOf
(
item
:
a
array
:
Array(a)
)
:
Number

Returns the index of the given item in the given array.

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

Inserts the given item into the given position of the given array.

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

Inserts the given element between the elements of the given array.

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

Returns whether or not the array is empty.

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

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

Array.last([]) == Maybe.nothing()
Array.last(["x", "a"]) == Maybe.just("a")
lastWithDefault
(
item
:
a
array
:
Array(a)
)
:
a

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

Array.lastWithDefault("a", []) == "a"
Array.lastWithDefault("a", ["x", "b"]) == "b"
map
(
func
:
Function(a, b)
array
:
Array(a)
)
:
Array(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]
mapWithIndex
(
func
:
Function(a, Number, b)
array
:
Array(a)
)
:
Array(b)

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(
  (index : Number, number : Number) : Number { number + index }, [1, 2, 3]) == [2, 4, 6]
max
(
array
:
Array(Number)
)
:
Number

Returns the maximum value of an array of numbers.

Array.max([0, 1, 2, 3, 4]) == 4
Array.max([]) == 0
move
(
from
:
Number
to
:
Number
array
:
Array(a)
)
:
Array(a)

Moves an item at the given 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(-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
(
item
:
a
array
:
Array(a)
)
:
Array(a)

Push an element to the end of an array.

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

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

Array.range(0, 5) == [0, 1, 2, 3, 4, 5]
reduce
(
initial
:
b
method
:
Function(b, a, b)
array
:
Array(a)
)
:
b

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
reduceRight
(
initial
:
b
func
:
Function(b, a, b)
array
:
Array(a)
)
:
b

Reduce a list from the right.

[1,2,3,4,5]
|> Array.reduceRight(0, (acc : Number, n : Number) : Number { acc + n}) == 15
reject
(
func
:
Function(a, Bool)
array
:
Array(a)
)
:
Array(a)

Returns all elements that does not matches the predicate function.

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

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]
sample
(
array
:
Array(a)
)
:
Maybe(a)

Returns an random element from the array.

Array.sample(["a"]) == Maybe.just("a")
Array.sample() == Maybe.nothing()
select
(
func
:
Function(a, Bool)
array
:
Array(a)
)
:
Array(a)

Returns all elements that matches the predicate function.

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

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]
size
(
array
:
Array(a)
)
:
Number

Returns the size of the array.

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

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

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

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]
sortBy
(
func
:
Function(a, b)
array
:
Array(a)
)
:
Array(a)

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]
sum
(
array
:
Array(Number)
)
:
Number

Sums up the given array of numbers.

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

Sums up the given array using the given function.

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

Spaws 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 indexs.

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

Take n number of items from the left.

Array.take(2, [1,2,3,4]) == [1,2]
unshift
(
item
:
a
array
:
Array(a)
)
:
Array(a)

Pushes a new item at the head of the array.

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

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]