Dictionaries and Sets
Dict stores key-value pairs; Set stores unique values.
Dict
Dict[K, V] maps keys to values and preserves insertion order for iteration.
dict.brp
import:
dict as D
scores: Dict[String, Int] = D.dict()Set
Set[T] tracks unique values.
set.brp
import:
set as S
seen: Set[String] = S.set()Lookup As Option
Dict.get returns Some(value) or None.
dict-get.brp
match scores.get("Ada"):
Some(score): print(score.to_string())
None: print("missing")Counting
Use get_or to read a previous count before writing the next one.
counting.brp
counts = counts.set(word, counts.get_or(word, 0) + 1)Grouping
Dictionaries are the natural shape for grouping derived values.
grouping.brp
groups = groups.set(initial, names_for_initial.append(name))Insertion Order
Dict iteration follows insertion order.
ordered-dict.brp
for (name, score) in scores:
print(name)Example
words.brp
import:
dict as D
set as S
func main(args: List[String]) -> Void:
words: List[String] = "one two one".words()
var counts: Dict[String, Int] = {}
var unique: Set[String] = S.set()
for word in words:
counts = D.set(counts, word, D.get_or(counts, word, 0) + 1)
unique = unique.add(word)
print(D
.get_or(counts, "one", 0)
.to_string())
print(unique
.to_list()
.join(", "))
Try It
terminal
blorp run words.brp