Skip to content

Advanced Collections

Tova provides specialized collection types in the collections namespace for common data structure patterns beyond arrays and objects.

OrderedDict

An insertion-ordered dictionary that maintains the order in which keys were added.

tova
d = collections.OrderedDict()

Methods

set

tova
d.set(key, value) -> OrderedDict

Sets a key-value pair. If the key exists, updates the value without changing order.

tova
d = collections.OrderedDict()
d.set("b", 2)
d.set("a", 1)
d.set("c", 3)

get

tova
d.get(key) -> T | Nil

Returns the value for a key, or nil if not found.

tova
d.get("a")    // 1
d.get("z")    // nil

delete

tova
d.delete(key) -> OrderedDict

Removes a key-value pair.

tova
d.delete("b")

has

tova
d.has(key) -> Bool

Returns true if the key exists.

tova
d.has("a")    // true
d.has("z")    // false

keys / values / entries

tova
d.keys() -> [String]
d.values() -> [T]
d.entries() -> [[String, T]]

Returns keys, values, or entries in insertion order.

tova
d.keys()       // ["b", "a", "c"]
d.values()     // [2, 1, 3]
d.entries()    // [["b", 2], ["a", 1], ["c", 3]]

length

tova
d.length -> Int

The number of entries.


DefaultDict

A dictionary that returns a default value for missing keys, generated by a factory function.

tova
d = collections.DefaultDict(fn() 0)

Constructor

tova
collections.DefaultDict(factory) -> DefaultDict

The factory is a function that produces the default value when a missing key is accessed.

tova
// Counter pattern
counter = collections.DefaultDict(fn() 0)

// List accumulator pattern
groups = collections.DefaultDict(fn() [])

Methods

get

tova
d.get(key) -> T

Returns the value for a key. If the key does not exist, calls the factory, stores the result, and returns it.

tova
counter = collections.DefaultDict(fn() 0)
counter.get("apples")     // 0 (created by factory)
counter.set("apples", counter.get("apples") + 1)
counter.get("apples")     // 1

set

tova
d.set(key, value) -> DefaultDict

Sets a key-value pair.

delete

tova
d.delete(key) -> DefaultDict

Removes a key-value pair.

has

tova
d.has(key) -> Bool

Returns true if the key exists (without triggering the factory).


Counter

A specialized collection for counting occurrences of items.

tova
c = collections.Counter()

Methods

count

tova
c.count(item) -> Counter

Increments the count for an item.

tova
c = collections.Counter()
c.count("apple")
c.count("banana")
c.count("apple")

total

tova
c.total() -> Int

Returns the sum of all counts.

tova
c.total()    // 3

most_common

tova
c.most_common(n?) -> [[T, Int]]

Returns the n most common items and their counts, sorted by frequency. If n is omitted, returns all items.

tova
c.most_common()     // [["apple", 2], ["banana", 1]]
c.most_common(1)    // [["apple", 2]]

keys / values / entries

tova
c.keys() -> [T]
c.values() -> [Int]
c.entries() -> [[T, Int]]

Returns items, counts, or item-count pairs.


Deque

A double-ended queue supporting efficient insertion and removal at both ends.

tova
dq = collections.Deque()

Methods

push_back / push_front

tova
dq.push_back(value) -> Deque
dq.push_front(value) -> Deque

Adds a value to the back or front.

tova
dq = collections.Deque()
dq.push_back(1)
dq.push_back(2)
dq.push_front(0)
// [0, 1, 2]

pop_back / pop_front

tova
dq.pop_back() -> T | Nil
dq.pop_front() -> T | Nil

Removes and returns the value at the back or front. Returns nil if empty.

tova
dq.pop_front()    // 0
dq.pop_back()     // 2

peek_back / peek_front

tova
dq.peek_back() -> T | Nil
dq.peek_front() -> T | Nil

Returns the value at the back or front without removing it.

length

tova
dq.length -> Int

The number of elements.

toArray

tova
dq.toArray() -> [T]

Converts the deque to an array.

tova
dq = collections.Deque()
dq.push_back(1)
dq.push_back(2)
dq.push_back(3)
dq.toArray()    // [1, 2, 3]

Examples

Word Frequency Counter

tova
text = "the cat sat on the mat the cat"
counter = collections.Counter()
for word in words(text) {
  counter.count(word)
}
counter.most_common(3)
// [["the", 3], ["cat", 2], ["sat", 1]]

Task Queue with Deque

tova
queue = collections.Deque()
queue.push_back("task1")
queue.push_back("task2")
queue.push_front("urgent-task")

loop {
  task = queue.pop_front()
  if task == nil { break }
  process(task)
}

Grouping with DefaultDict

tova
groups = collections.DefaultDict(fn() [])
for user in users {
  items = groups.get(user.role)
  groups.set(user.role, [...items, user])
}

Released under the MIT License.