Collections

Manipulate arrays and maps efficiently in Vadalog programs.

Array Operations

collections:size()

Get the number of elements in an array:
collections:size(arr: array<any>) → int
skilled_employees(Name, SkillCount) <- 
    employee(Name, Skills), 
    SkillCount = collections:size(Skills), 
    SkillCount > 3.

collections:is_empty()

Check if an array is empty:
collections:is_empty(arr: array<any>) → boolean
emptyList(ID) <- 
    data(ID, List), 
    collections:is_empty(List).

collections:contains()

Check if an array contains a specific value:
collections:contains(arr: array<any>, value: any) → boolean
java_developers(Name) <- 
    employee(Name, Skills), 
    HasJava = collections:contains(Skills, "Java"), 
    HasJava == #T.

collections:contains_all()

Check if an array contains all elements from another array:
collections:contains_all(arr: array<any>, subset: array<any>) → boolean
qualified_candidates(Name) <- 
    candidate(Name, Skills), 
    RequiredSkills = ["Java", "Python"], 
    HasAll = collections:contains_all(Skills, RequiredSkills), 
    HasAll == #T.

collections:add()

Add an element to an array:
collections:add(arr: array<T>, elem: T) → array<T>
add_skill(Name, Skills, UpdatedSkills) <- 
    employee(Name, Skills), 
    UpdatedSkills = collections:add(Skills, "Leadership").

collections:remove()

Remove all occurrences of a value from an array:
collections:remove(arr: array<T>, elem: T) → array<T>
cleaned(Result) <- 
    data([1, 2, 3, 2, 4]), 
    Result = collections:remove([1, 2, 3, 2, 4], 2).
% Result: [1, 3, 4]

collections:get()

Get an element at a specific index (1-based):
collections:get(arr: array<T>, index: int) → T
first_skill(Name, FirstSkill) <- 
    employee(Name, Skills), 
    FirstSkill = collections:get(Skills, 1).

Set Operations on Arrays

collections:union()

Combine two arrays, removing duplicates:
collections:union(a: array<T>, b: array<T>) → array<T>

collections:difference()

Get elements in first array but not in second:
collections:difference(a: array<T>, b: array<T>) → array<T>

collections:intersection()

Get common elements between two arrays:
collections:intersection(a: array<T>, b: array<T>) → array<T>

Array Transformations

collections:sort()

Sort array elements in ascending or descending order:
collections:sort(arr: array<T>) → array<T>
collections:sort(arr: array<T>, order: string) → array<T>
The optional second parameter specifies sort order: "asc" (default) or "desc".
data([3, 1, 4, 2]).
sorted_asc(Result) <- data(List), Result = collections:sort(List, "asc").
% Result: [1, 2, 3, 4]

fruits(["mango", "apple", "zebra", "banana"]).
sorted_desc(Result) <- fruits(List), Result = collections:sort(List, "desc").
% Result: ["zebra", "mango", "banana", "apple"]

collections:distinct()

Remove duplicate elements from an array:
collections:distinct(arr: array<T>) → array<T>

collections:remove_nulls()

Remove all null values from an array:
collections:remove_nulls(arr: array<T>) → array<T>

collections:shuffle()

Randomly shuffle array elements:
collections:shuffle(arr: array<T>) → array<T>

Advanced Array Operations

collections:explode()

Expand an array into multiple rows (one per element):
collections:explode(arr: array<T>) → T
exploded(Element) <- 
    fruits(["apple", "banana", "cherry"]), 
    Element = collections:explode(["apple", "banana", "cherry"]).
% Results in 3 rows: "apple", "banana", "cherry"

collections:flatten()

Transform an array of arrays into a single array:
collections:flatten(arr: array<T>) → T
nested_elements([[1, 2], [3, 4]])
flattened(Element) <- 
    nested_elements(X), 
    Element = collections:flatten(X).
% Result: [1, 2, 3, 4]

collections:slice()

Extract a slice of an array:
collections:slice(arr: array<T>, start: int, length: int) → array<T>
slice_example(Result) <- 
    fruits(["apple", "banana", "cherry", "date", "elderberry"]), 
    Result = collections:slice(["apple", "banana", "cherry", "date", "elderberry"], 2, 3).
% Result: ["banana", "cherry", "date"]

collections:sub_array()

Get first N elements from an array:
collections:sub_array(arr: array<T>, length: int) → array<T>

collections:transform()

Apply a transformation function to each element:
collections:transform(arr: array<T>, lambdaSql: string) → array<any>
% Add 1 to each element
increment_all(Transformed) <- 
    input([1, 2, 3]), 
    Transformed = collections:transform([1, 2, 3], "x -> x + 1").
% Result: [2, 3, 4]

% Transform with index (element + index)
with_index(Transformed) <- 
    input([1, 2, 3]), 
    Transformed = collections:transform([1, 2, 3], "(x, i) -> x + i").
% Result: [1, 3, 5]

collections:filter()

Filter array elements based on a condition:
collections:filter(arr: array<T>, lambdaSql: string) → array<T>
% Filter elements greater than 2
filter_large(Filtered) <- 
    input([1, 2, 3, 4]), 
    Filtered = collections:filter([1, 2, 3, 4], "x -> x > 2").
% Result: [3, 4]

% Filter with index (keep elements at odd indices)
filter_odd_indices(Filtered) <- 
    input([10, 20, 30, 40]), 
    Filtered = collections:filter([10, 20, 30, 40], "(x, i) -> i % 2 != 0").
% Result: [20, 40]

Map Operations

collections:map()

Create a map from key-value pairs:
collections:map(k1: K, v1: V, k2: K, v2: V, …) → map<K,V>
user_metadata(UserID, Metadata) <- 
    user(UserID, Name, Age), 
    Metadata = collections:map("name", Name, "age", Age, "id", UserID).