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
Example:
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
Example:
% Find all records with empty lists using implicit boolean condition
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
Example:
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
Example:
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>
Example:
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>
Example:
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
Example:
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>
Example:
combined_skills(Dept, AllSkills) <-
dept_skills(Dept, Skills1),
other_dept_skills(Dept, Skills2),
AllSkills = collections:union(Skills1, Skills2).
collections:difference()
Get elements in first array but not in second:
collections:difference(a: array<T>, b: array<T>) → array<T>
Example:
unique_skills(Name, UniqueSkills) <-
employee(Name, Skills),
common_skills(CommonSkills),
UniqueSkills = collections:difference(Skills, CommonSkills).
collections:intersection()
Get common elements between two arrays:
collections:intersection(a: array<T>, b: array<T>) → array<T>
Example:
shared_skills(Name1, Name2, SharedSkills) <-
employee(Name1, Skills1),
employee(Name2, Skills2),
SharedSkills = collections:intersection(Skills1, Skills2).
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 the sort order:
"asc"- Sort in ascending order (default)"desc"- Sort in descending order
Examples:
% Sort in ascending order (default)
sorted_asc(Name, SortedScores) <-
test_scores(Name, Scores),
SortedScores = collections:sort(Scores).
% Sort numbers in ascending order
data([3, 1, 4, 2]).
sorted_desc(Result) <-
data(List),
Result = collections:sort(List, "asc").
% Result: [1, 2, 3, 4]
% Sort strings in descending order
fruits(["mango", "apple", "zebra", "banana"]).
sorted_strings(Result) <-
fruits(List),
Result = collections:sort(List, "desc").
% Result: ["zebra", "mango", "banana", "apple"]
collections:distinct()
Remove duplicate elements from array:
collections:distinct(arr: array<T>) → array<T>
Example:
unique_categories(Product, UniqueCategories) <-
product_tags(Product, Tags),
UniqueCategories = collections:distinct(Tags).
collections:remove_nulls()
Remove all null values from array:
collections:remove_nulls(arr: array<T>) → array<T>
Example:
clean_data(Name, CleanedValues) <-
data_with_nulls(Name, Values),
CleanedValues = collections:remove_nulls(Values).
collections:shuffle()
Randomly shuffle array elements:
collections:shuffle(arr: array<T>) → array<T>
Example:
randomized_questions(Quiz, RandomOrder) <-
quiz_questions(Quiz, Questions),
RandomOrder = collections:shuffle(Questions).
Advanced Array Operations
collections:explode()
Expand array into multiple rows (one per element):
collections:explode(arr: array<T>) → T
Example:
exploded(Element) <-
fruits(["apple", "banana", "cherry"]),
Element = collections:explode(["apple", "banana", "cherry"]).
% Results in 3 rows: "apple", "banana", "cherry"