Collections
Draft provides three primary collection types: arrays, maps, and sets.
Arrays
Section titled “Arrays”Ordered, indexed collections of same-type elements.
Creating Arrays
Section titled “Creating Arrays”@numbers: Array<Int> = [1, 2, 3, 4, 5]@names: Array<String> = ["Alice", "Bob", "Charlie"]@empty: Array<Float> = []Array Literals
Section titled “Array Literals”@list = [1, 2, 3]@repeated = [0; 10] @// Array of 10 zerosAccessing Elements
Section titled “Accessing Elements”@first = @list[0] @// 1@last = @list[@list#length - 1] @// 3Modifying Arrays
Section titled “Modifying Arrays”mut @list = [1, 2, 3]@list[0] = 10 @// [10, 2, 3]@list#push(4) @// [10, 2, 3, 4]@list#pop() @// Returns 4, list is [10, 2, 3]@list#insert(1, 99) @// [10, 99, 2, 3]@list#remove(1) @// Removes element at index 1Array Properties
Section titled “Array Properties”@list#length @// Number of elements@list#is_empty @// true if length == 0@list#first @// First element@list#last @// Last elementArray Methods
Section titled “Array Methods”@list#slice(1, 3) @// Sub-array from index 1 to 3@list#reverse() @// Reversed copy@list#sort() @// Sorted copy@list#map(fn(@x) => @x * 2) @// Transform each element@list#filter(fn(@x) => @x > 2) @// Keep matching elements@list#reduce(0, fn(@acc, @x) => @acc + @x) @// Sum all@list#contains(3) @// true if element exists@list#index_of(3) @// Index of first occurrenceIterating Arrays
Section titled “Iterating Arrays”for @item in @list write(@item)fin for.
for @index, @item in @list write(string(@index) + ": " + string(@item))fin for.Key-value pair collections.
Creating Maps
Section titled “Creating Maps”@scores: Map<String, Int> = {"Alice": 95, "Bob": 87}@empty: Map<String, String> = {}Accessing Values
Section titled “Accessing Values”@score = @scores["Alice"] @// 95@maybe = @scores#get("Dave") @// Option<Int>Modifying Maps
Section titled “Modifying Maps”mut @scores = {"Alice": 95}@scores["Bob"] = 87 @// Insert or update@scores#insert("Charlie", 92) @// Insert@scores#remove("Alice") @// Remove keyMap Properties
Section titled “Map Properties”@scores#length @// Number of entries@scores#is_empty @// true if empty@scores#keys @// Array of keys@scores#values @// Array of values@scores#contains("Alice") @// true if key existsIterating Maps
Section titled “Iterating Maps”for @key, @value in @scores write(@key + ": " + string(@value))fin for.Collections of unique elements.
Creating Sets
Section titled “Creating Sets”@unique: Set<Int> = {1, 2, 3, 4, 5}@letters: Set<Char> = {'a', 'b', 'c'}Set Operations
Section titled “Set Operations”@a = {1, 2, 3}@b = {2, 3, 4}
@a#union(@b) @// {1, 2, 3, 4}@a#intersection(@b) @// {2, 3}@a#difference(@b) @// {1}@symmetric_diff(@a, @b) @// {1, 4}Modifying Sets
Section titled “Modifying Sets”mut @set = {1, 2, 3}@set#add(4) @// Add element@set#remove(2) @// Remove element@set#contains(3) @// trueIterating Sets
Section titled “Iterating Sets”for @item in @set write(@item)fin for.Performance Characteristics
Section titled “Performance Characteristics”| Operation | Array | Map | Set |
|---|---|---|---|
| Access | O(1) | O(1) | O(1) |
| Insert | O(1)* | O(1) | O(1) |
| Remove | O(n) | O(1) | O(1) |
| Search | O(n) | O(1) | O(1) |
*Amortized for push at end
Choosing a Collection
Section titled “Choosing a Collection”| Use Case | Collection |
|---|---|
| Ordered list of items | Array |
| Fast lookup by key | Map |
| Unique items, membership test | Set |
| Stack (LIFO) | Array (push/pop) |
| Queue (FIFO) | Array (push/shift) |
Next Steps
Section titled “Next Steps”Continue to Strings for string manipulation.
