p-array
The array module provides utility functions for working with arrays.
use p-array as aType Check
Section titled “Type Check”isarray[val]
Section titled “isarray[val]”Returns true if val is an array.
use p-array as aa.isarray[list[1; 2; 3]] -- truea.isarray[42] -- falseCreation
Section titled “Creation”from[val]
Section titled “from[val]”Creates an array from an iterable or array-like value.
of[...args]
Section titled “of[...args]”Creates an array from the given arguments.
range[start; end; step]
Section titled “range[start; end; step]”Creates an array of numbers from start to end (exclusive). step defaults to 1.
use p-array as aa.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]len[arr]
Section titled “len[arr]”Returns the number of elements.
first[arr]
Section titled “first[arr]”Returns the first element.
last[arr]
Section titled “last[arr]”Returns the last element.
use p-array as aconst xs be list[10; 20; 30]a.len[xs] -- 3a.first[xs] -- 10a.last[xs] -- 30Transform
Section titled “Transform”flatten[arr; depth]
Section titled “flatten[arr; depth]”Flattens nested arrays to the specified depth (default: all levels).
unique[arr]
Section titled “unique[arr]”Returns a new array with duplicate values removed.
compact[arr]
Section titled “compact[arr]”Returns a new array with all falsy values removed.
sortasc[arr]
Section titled “sortasc[arr]”Returns a new array sorted in ascending numeric order.
sortdesc[arr]
Section titled “sortdesc[arr]”Returns a new array sorted in descending numeric order.
use p-array as aa.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]Grouping
Section titled “Grouping”chunk[arr; size]
Section titled “chunk[arr; size]”Splits the array into chunks of the given size.
zip[...arrs]
Section titled “zip[...arrs]”Zips multiple arrays into an array of tuples.
unzip[arr]
Section titled “unzip[arr]”Unzips an array of tuples into separate arrays.
groupby[arr; fn]
Section titled “groupby[arr; fn]”Groups elements by the result of calling fn on each element.
use p-array as aa.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] }Aggregation
Section titled “Aggregation”sum[arr]
Section titled “sum[arr]”Returns the sum of all elements.
product[arr]
Section titled “product[arr]”Returns the product of all elements.
min[arr]
Section titled “min[arr]”Returns the smallest element.
max[arr]
Section titled “max[arr]”Returns the largest element.
count[arr; fn]
Section titled “count[arr; fn]”Returns the number of elements for which fn returns true.
use p-array as aconst xs be list[1; 2; 3; 4; 5]a.sum[xs] -- 15a.product[xs] -- 120a.min[xs] -- 1a.max[xs] -- 5a.count[xs; fn x to x gt 2] -- 3