09 · Data Structures
Map comprehensions
Build maps with for and if entries inside #{ }. Computed keys use [expression]: value. Iterables, keys and values evaluate once in source order; duplicate keys use ordinary map replacement behavior. Loop and filter bindings stay within their entry.
On this page
Build maps with for and if entries inside #{ }. Computed keys use [expression]: value. Iterables, keys and values evaluate once in source order; duplicate keys use ordinary map replacement behavior. Loop and filter bindings stay within their entry.
Complete example
Map comprehensions.
map_builders.zolo
struct User { id: int, name: str, enabled: bool }
let users = [
User { id: 1, name: "Ada", enabled: true },
User { id: 2, name: "Lin", enabled: false },
]
let names = #{for user in users => [user.id]: user.name}
let active_names = #{for user in users => if user.enabled => [user.id]: user.name}
print(names[2])
print(active_names[1])
Rules and behavior
Records, maps and updates · Documentation
From a checkout of the repository:
zolo examples/features/map_builders.zolo
See also