Alpha. Vary is under active development and not ready for production use. Syntax, APIs, performance, and behaviour may change between releases.

Collections

The collections module provides generic higher-order functions for working with lists, dicts, and sets. Import individual functions rather than the whole module:

from collections import first, filter, group_by

let nums = [3, 1, 4, 1, 5, 9, 2, 6]

let evens = filter(nums, lambda n: Int: n % 2 == 0)
let first_even = first(evens)

let words = ["apple", "avocado", "banana", "blueberry", "cherry"]
let by_letter = group_by(words, lambda w: Str: w[0])
# {"a": ["apple", "avocado"], "b": ["banana", "blueberry"], "c": ["cherry"]}

Note: These are free functions imported from the collections module, not methods on collection types. Built-in instance methods (.append(), .keys(), .add(), etc.) are documented separately.

List utilities

FunctionReturnsDescription
first(items)TFirst element; raises if empty
last(items)TLast element; raises if empty
is_empty(items)BoolTrue if the list has no elements
index_of(items, value)IntIndex of first occurrence, or -1 if not found
map(items, fn)List[U]Apply fn to every element, collect results
filter(items, fn)List[T]Keep elements where fn returns True
flat_map(items, fn)List[U]Map then flatten one level
reduce(items, init, fn)UFold list into a single value starting from init
any(items, fn)BoolTrue if fn returns True for at least one element
all(items, fn)BoolTrue if fn returns True for every element
find(items, fn)T?First element where fn is True, or None
unique(items)List[T]Remove duplicates, preserving first occurrence order
chunk(items, size)List[List[T]]Split into sub-lists of at most size elements
flatten(items)List[T]Flatten one level of nesting
sorted(items)List[T]Return a new sorted list (natural order)
sorted_by(items, key)List[T]Return a new list sorted by the result of key
group_by(items, key)Dict[K, List[T]]Partition elements by the result of key
from collections import map, filter, reduce, sorted_by, find, chunk

let scores = [72, 95, 88, 41, 63, 99]

let passing = filter(scores, lambda s: Int: s >= 70)
# [72, 95, 88, 63, 99]

let doubled = map(passing, lambda s: Int: s * 2)
# [144, 190, 176, 126, 198]

let total = reduce(scores, 0, lambda acc: Int, s: Int: acc + s)
# 458

let ranked = sorted_by(scores, lambda s: Int: -s)
# [99, 95, 88, 72, 63, 41]

let first_failing = find(scores, lambda s: Int: s < 70)
# 41

let pages = chunk(scores, 2)
# [[72, 95], [88, 41], [63, 99]]

Dict utilities

FunctionReturnsDescription
keys(d)List[K]All keys as a list
values(d)List[V]All values as a list
items(d)List[(K, V)]All key-value pairs as a list of tuples
invert(d)Dict[V, K]Swap keys and values
merge(left, right)Dict[K, V]Combine two dicts; right wins on conflicts
get_or(d, key, default)VLook up key, returning default if absent
from collections import keys, values, items, invert, merge, get_or

let config = {"host": "localhost", "port": "8080", "env": "dev"}

let all_keys = keys(config)
# ["host", "port", "env"]

let overrides = {"port": "9090", "debug": "true"}
let merged = merge(config, overrides)
# {"host": "localhost", "port": "9090", "env": "dev", "debug": "true"}

let port = get_or(merged, "timeout", "30")
# "30"

let codes = {"ok": 200, "not_found": 404}
let by_code = invert(codes)
# {200: "ok", 404: "not_found"}

Set utilities

FunctionReturnsDescription
union(a, b)Set[T]All elements in either set
intersection(a, b)Set[T]Only elements present in both sets
difference(a, b)Set[T]Elements in a but not in b
is_subset(a, b)BoolTrue if every element of a is also in b
from collections import union, intersection, difference, is_subset

let backend = {"alice", "bob", "carol"}
let frontend = {"bob", "carol", "dave"}

let team = union(backend, frontend)
# {"alice", "bob", "carol", "dave"}

let fullstack = intersection(backend, frontend)
# {"bob", "carol"}

let backend_only = difference(backend, frontend)
# {"alice"}

print(is_subset(fullstack, backend))
# True