Alpha. Vary is under active development and not ready for production use. Syntax, APIs, performance, and behaviour may change between releases.
Collections and strings
Vary has three collection types: lists, dicts, and sets. All are generic and mutable, and all have literal syntax so you can write [1, 2, 3] or {"a": 1} directly. Strings are covered here too since they support the same indexing and slicing operations.
Lists
let nums: List[Int] = [1, 2, 3, 4, 5]
print(nums[0]) # 1
print(nums[1:3]) # [2, 3]
print(len(nums)) # 5
mut items: List[Int] = [1, 2]
items.append(3)
items.pop()
| Method | Description |
|---|---|
append(x) | Add element to end |
pop() | Remove and return last element |
insert(i, x) | Insert at index |
remove(x) | Remove first occurrence |
sort() | Sort in place |
reverse() | Reverse in place |
index(x) | Index of first occurrence |
count(x) | Count occurrences |
contains(x) | Check if element exists |
copy() | Shallow copy |
clear() | Remove all elements |
extend(other) | Append all from other list |
Collection pipelines
List methods that take a lambda for functional-style data processing:
| Method | Returns | Description |
|---|---|---|
filter(fn) | List[T] | Keep elements where fn returns True |
map(fn) | List[U] | Transform each element |
flat_map(fn) | List[U] | Transform each element to a list and flatten |
find(fn) | T? | First element matching predicate, or None |
any(fn) | Bool | True if any element matches |
all(fn) | Bool | True if all elements match |
count_where(fn) | Int | Count of elements matching predicate |
first_or_none() | T? | First element, or None if empty |
sorted_by(fn) | List[T] | New list sorted by key function |
let names = ["alice", "bob", "charlie"]
let long_names = names.filter(lambda s: Str: len(s) > 3)
let upper = names.map(lambda s: Str: s.upper())
let has_bob = names.any(lambda s: Str: s == "bob")
group_by is a standalone builtin function:
let groups = group_by(items, lambda item: Item: item.category)
# Returns Dict[Str, List[Item]]
Dictionaries
let ages: Dict[Str, Int] = {"Alice": 30, "Bob": 25}
print(ages["Alice"]) # 30
| Method | Description |
|---|---|
get(key) | Get value (returns None if missing) |
keys() | List of keys |
values() | List of values |
items() | List of key-value pairs |
pop(key) | Remove and return value |
update(other) | Merge another dict |
copy() | Shallow copy |
clear() | Remove all entries |
Sets
mut s: Set[Int] = {1, 2, 3}
s.add(4)
s.discard(1)
| Method | Description |
|---|---|
add(x) | Add element |
remove(x) | Remove element (error if missing) |
discard(x) | Remove element (no error if missing) |
contains(x) | Check membership |
union(other) | Set union |
intersection(other) | Set intersection |
difference(other) | Set difference |
Set operators: a | b (union), a & b (intersection), a - b (difference), a |= b (union in place).
Strings
let name = "World"
print(f"Hello, {name}!")
let s = "Hello, World!"
print(s[0]) # "H"
print(s[0:5]) # "Hello"
print(s[7:]) # "World!"
print(s[:5]) # "Hello"
| Method | Description |
|---|---|
upper() | Uppercase |
lower() | Lowercase |
strip() | Trim whitespace |
split(sep) | Split into list |
join(list) | Join list with separator |
replace(old, new) | Replace occurrences |
find(sub) | Index of substring (-1 if not found) |
contains(sub) | Check if substring exists |
startswith(prefix) | Check prefix |
endswith(suffix) | Check suffix |
count(sub) | Count occurrences |
capitalize() | Capitalize first letter |
isalpha() | All alphabetic |
isdigit() | All digits |
partition(sep) | Split into (before, sep, after) tuple |
splitlines() | Split into list of lines |