Collections
Vertex has three built-in collection types: List<T>, Map<K, V>, and Set<T>.
Lists are the most common collection. They are ordered and generic:
let names: List<String> = ["Alice", "Bob", "Carol"]let empty: List<String> = []
debug names.size() // 3debug names.isEmpty() // falseTransforming lists
Section titled “Transforming lists”let numbers: List<Int> = [1, 2, 3, 4, 5]
let doubled = numbers.map(fn(n) { n * 2 }) // (2, 4, 6, 8, 10)let evens = numbers.filter(fn(n) { n % 2 == 0 }) // (2, 4)let sum = numbers.fold(0, fn(acc, n) { acc + n }) // 15Safe access
Section titled “Safe access”There is no subscript syntax (list[i]). Index access returns Option<T>:
let xs = [10, 20, 30]debug xs.get(1) // Some(value: 20)debug xs.get(99) // Nonedebug xs.first() // Some(value: 10)debug xs.last() // Some(value: 30)Spread to combine
Section titled “Spread to combine”let a = [1, 2, 3]let b = [4, 5, 6]let combined = [...a, ...b, 7] // (1, 2, 3, 4, 5, 6, 7)Iterating with for..in
Section titled “Iterating with for..in”for name in names { debug name}
// With indexfor #(i, name) in names.enumerate() { debug "${i}: ${name}"}Factories
Section titled “Factories”let squares = List.generate(5, fn(i: Int): Int { i * i })// (0, 1, 4, 9, 16)
let zeros = List.filled(4, 0)// (0, 0, 0, 0)Maps are key-value stores. There is no subscript syntax. Use get(key):
let scores: Map<String, Int> = {"Alice": 95, "Bob": 87}let empty: Map<String, Int> = Map.empty()
debug scores.get("Alice") // Some(value: 95)debug scores.get("Carol") // None
let updated = scores.put("Carol", 92) // returns a new map. Original unchangedfor #(name, score) in updated.entries() { debug "${name}: ${score}"}Sets are unordered collections with no duplicates, using #{} literal syntax:
let tags: Set<String> = #{"apex", "salesforce", "vertex"}
debug tags.contains("apex") // truedebug tags.size() // 3
let extended = tags.add("Dart")let filtered = tags.filter(fn(t) { t.length() > 4 })
let a = #{1, 2, 3}let b = #{3, 4, 5}debug a.union(b) // #{1, 2, 3, 4, 5}debug a.intersection(b) // #{3}debug a.difference(b) // #{1, 2}Next steps
Section titled “Next steps”- Collections reference: full method tables for List, Map, Set
- for..in Loop: pattern destructuring in loop heads