Skip to content

p-array

The array module provides utility functions for working with arrays.

use p-array as a

Returns true if val is an array.

use p-array as a
a.isarray[list[1; 2; 3]] -- true
a.isarray[42] -- false

Creates an array from an iterable or array-like value.

Creates an array from the given arguments.

Creates an array of numbers from start to end (exclusive). step defaults to 1.

use p-array as a
a.from[///hello///] -- ["h", "e", "l", "l", "o"]
a.of[1; 2; 3] -- [1, 2, 3]
a.range[0; 5] -- [0, 1, 2, 3, 4]
a.range[0; 10; 2] -- [0, 2, 4, 6, 8]

Returns the number of elements.

Returns the first element.

Returns the last element.

use p-array as a
const xs be list[10; 20; 30]
a.len[xs] -- 3
a.first[xs] -- 10
a.last[xs] -- 30

Flattens nested arrays to the specified depth (default: all levels).

Returns a new array with duplicate values removed.

Returns a new array with all falsy values removed.

Returns a new array sorted in ascending numeric order.

Returns a new array sorted in descending numeric order.

use p-array as a
a.flatten[[[1; 2]; [3; [4; 5]]]] -- [1, 2, 3, 4, 5]
a.unique[list[1; 2; 2; 3; 1]] -- [1, 2, 3]
a.compact[list[0; 1; null; 2; //////; 3]] -- [1, 2, 3]
a.sortasc[list[3; 1; 2]] -- [1, 2, 3]
a.sortdesc[list[3; 1; 2]] -- [3, 2, 1]

Splits the array into chunks of the given size.

Zips multiple arrays into an array of tuples.

Unzips an array of tuples into separate arrays.

Groups elements by the result of calling fn on each element.

use p-array as a
a.chunk[list[1; 2; 3; 4; 5]; 2] -- [[1, 2], [3, 4], [5]]
a.zip[list[1; 2]; list[///a///; ///b///]] -- [[1, "a"], [2, "b"]]
a.groupby[list[1; 2; 3; 4; 5]; fn x to
if x mod 2 eq 0 then ///even/// else ///odd///
]
-- { odd: [1, 3, 5], even: [2, 4] }

Returns the sum of all elements.

Returns the product of all elements.

Returns the smallest element.

Returns the largest element.

Returns the number of elements for which fn returns true.

use p-array as a
const xs be list[1; 2; 3; 4; 5]
a.sum[xs] -- 15
a.product[xs] -- 120
a.min[xs] -- 1
a.max[xs] -- 5
a.count[xs; fn x to x gt 2] -- 3