Array Functions¶
Array manipulation, transformation, filtering, and aggregation functions. Most are instance methods called on array values.
Array Creation¶
$Array¶
Creates an array from values.
Signature: $Array(value1, value2, ...)
Examples:
Filtering & Finding¶
.Filter(predicate)¶
Filters array elements matching predicate.
Signature: array.Filter(element => condition)
Examples:
numbers = [1, 2, 3, 4, 5, 6];
evens = numbers.Filter((x) => x % 2 == 0); // [2, 4, 6]
customers = getCustomers();
active = customers.Filter((c) => c.isActive);
.Find(predicate)¶
Returns first element matching predicate.
Signature: array.Find(element => condition)
Examples:
numbers = [1, 2, 3, 4, 5];
firstEven = numbers.Find((x) => x % 2 == 0); // 2
customers = getCustomers();
alice = customers.Find((c) => c.name == "Alice");
.Any(predicate)¶
Checks if any element matches.
Signature: array.Any(element => condition)
Returns: (boolean)
Examples:
numbers = [1, 2, 3, 4, 5];
hasEvens = numbers.Any((x) => x % 2 == 0); // true
hasNegatives = numbers.Any((x) => x < 0); // false
.All(predicate)¶
Checks if all elements match.
Signature: array.All(element => condition)
Returns: (boolean)
Examples:
numbers = [2, 4, 6, 8];
allEven = numbers.All((x) => x % 2 == 0); // true
allPositive = numbers.All((x) => x > 0); // true
Transformation¶
.Map(transform)¶
Transforms each element.
Signature:
Examples:
numbers = [1, 2, 3, 4, 5];
doubled = numbers.Map((x) => x * 2); // [2, 4, 6, 8, 10]
customers = getCustomers();
names = customers.Map((c) => c.name);
withIndex = numbers.Map((x, i) => x + i); // [1, 3, 5, 7, 9]
Reduction¶
.ReduceToNum(fn, initial)¶
Reduces array to single number.
Signature:
array.ReduceToNum((accumulator, element) => result, initialValue);
array.ReduceToNum((accumulator, element, index) => result, initialValue);
Examples:
numbers = [1, 2, 3, 4, 5];
sum = numbers.ReduceToNum((acc, x) => acc + x, 0); // 15
product = numbers.ReduceToNum((acc, x) => acc * x, 1); // 120
max = numbers.ReduceToNum((acc, x) => (x > acc ? x : acc), 0); // 5
.Reduce(fn, initial)¶
Reduces array to single string value.
Signature: array.Reduce((accumulator, element) => result, initialValue)
Examples:
words = ["hello", "world"];
sentence = words.Reduce((acc, w) => acc + " " + w, "");
numbers = [1, 2, 3];
csv = numbers.Reduce((acc, n) => acc + "," + $ToString(n), "");
.ReduceToArr(fn, initial)¶
Reduces to array.
Signature: array.ReduceToArr((accumulator, element) => result, initialValue)
Examples:
numbers = [1, 2, 3];
doubled = numbers.ReduceToArr((acc, x) => {
acc.Push(x * 2);
return acc;
}, []);
Sorting¶
.OrderBy(selector)¶
Sorts ascending by selector.
Signature: array.OrderBy(element => sortValue)
Examples:
numbers = [5, 2, 8, 1, 9];
sorted = numbers.OrderBy((x) => x); // [1, 2, 5, 8, 9]
customers = getCustomers();
byAge = customers.OrderBy((c) => c.age);
byName = customers.OrderBy((c) => c.name);
.OrderByDescending(selector)¶
Sorts descending by selector.
Signature: array.OrderByDescending(element => sortValue)
Examples:
numbers = [5, 2, 8, 1, 9];
sorted = numbers.OrderByDescending((x) => x); // [9, 8, 5, 2, 1]
byPremium = customers.OrderByDescending((c) => c.premium);
Slicing & Pagination¶
.Take(count)¶
Takes first N elements.
Signature: array.Take(count)
Examples:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
first3 = numbers.Take(3); // [1, 2, 3]
top5 = customers.OrderByDescending((c) => c.premium).Take(5);
.Skip(count)¶
Skips first N elements.
Signature: array.Skip(count)
Examples:
numbers = [1, 2, 3, 4, 5];
rest = numbers.Skip(2); // [3, 4, 5]
page2 = items.Skip(10).Take(10); // Pagination
.Slice(start, end)¶
Extracts slice from start to end index.
Signature: array.Slice(startIndex, endIndex)
Examples:
Adding Elements¶
.Push(value)¶
Adds element to end.
Signature: array.Push(value)
Examples:
items = [1, 2, 3];
items.Push(4); // [1, 2, 3, 4]
items.Push(5);
items.Push(6); // [1, 2, 3, 4, 5, 6]
.Insert(index, value)¶
Inserts element at index.
Signature: array.Insert(index, value)
Examples:
.AddRange(otherArray)¶
Adds all elements from another array.
Signature: array.AddRange(otherArray)
Returns: New array with combined elements
Examples:
Removing Elements¶
.Remove(value)¶
Removes first occurrence of value.
Signature: array.Remove(value)
Examples:
.RemoveAt(index)¶
Removes element at index.
Signature: array.RemoveAt(index)
Examples:
.Clear()¶
Removes all elements.
Signature: array.Clear()
Examples:
Distinct & Unique¶
.Distinct()¶
Returns unique elements.
Signature: array.Distinct()
Examples:
numbers = [1, 2, 2, 3, 3, 3, 4, 5, 5];
unique = numbers.Distinct(); // [1, 2, 3, 4, 5]
ids = customers.Map((c) => c.departmentId).Distinct();
Iteration¶
.ForEach(action)¶
Executes action for each element.
Signature:
Examples:
names = ["Alice", "Bob", "Charlie"];
names.ForEach((name) => $Log(name));
names.ForEach((name, i) => {
$Log($ToString(i) + ": " + name);
});
Searching¶
.IndexOf(value)¶
Returns first index of value, or -1 if not found.
Signature: array.IndexOf(value)
Examples:
Reversal¶
.Reverse()¶
Reverses array order.
Signature: array.Reverse()
Examples:
Joining¶
.Join(separator)¶
Joins elements into string.
Signature: array.Join(separator)
Examples:
words = ["hello", "world"];
sentence = words.Join(" "); // "hello world"
numbers = [1, 2, 3];
csv = numbers.Join(","); // "1,2,3"
Property Access¶
.Length¶
Number of elements in array.
Signature: array.Length
Examples:
Common Patterns¶
Sum¶
numbers = [1, 2, 3, 4, 5];
sum = numbers.ReduceToNum((acc, x) => acc + x, 0);
// Or: sum = $Sum(numbers);
Average¶
Max/Min¶
numbers = [5, 2, 8, 1, 9];
max = numbers.ReduceToNum((acc, x) => (x > acc ? x : acc), numbers[0]);
min = numbers.ReduceToNum((acc, x) => (x < acc ? x : acc), numbers[0]);
Count Matching¶
Flatten Nested Arrays¶
nested = [
[1, 2],
[3, 4],
[5, 6],
];
flat = nested.ReduceToArr((acc, arr) => acc.AddRange(arr), []);
Grouping¶
items = getItems();
low = items.Filter((x) => x.value < 10);
medium = items.Filter((x) => x.value >= 10 && x.value < 100);
high = items.Filter((x) => x.value >= 100);
Pagination¶
Best Practices¶
- Chain operations for readability
- Use .Any() instead of
.Length > 0for existence checks - Prefer native functions over manual loops
- Extract complex lambdas to named functions
- Check .Length before accessing elements