Maps, Keywords, and Sets
Create a map literal:
Create a map using a function:
Retrieve a value from a map by its key:
Same, but using the map like a function:
If the key is not found, nil is returned:
Use keywords instead of strings as map keys:
Use the keyword like a function for map lookup:
Add a new entry (key-value pair) to a map:
Add multiple new entries to a map:
Remove entries from a map:
Non-existant keys are ignored silently:
Retrieve all the keys and values, respectively, from a map:
The order of keys is not guaranteed, but matches the order of the values. Use
sorted-map to create a map with keys kept in sorted order.
Check whether or not a map contains a key:
Create a set literal:
Create a set using a function:
Check whether or not a set contains a value:
Lookup of keywords like a function:
Extend a set:
Remove a value from a set:
Exercises
Change a Map
Given the following map:
Write an expression to remove the :uptime key (with its value) from the map,
to update the :active value to false in the map, and to add the key
:decommissioned with the value 2025 to the map.
Hint: Use the assoc function to alter and add a key-value pair, and the disj
function to remove a key-value pair.
Test: The map {:name "Persephone" :active false :decommissioned 2025} is returned.
Change a Set
Given the following set:
Write an expression to remove the value "Adrian" from the set, and to add the
value "Janick" to the set.
Hint: Use the disj function to remove a value, and the conj function to add
a value.
Test: The set {"Steve" "Dave" "Bruce" "Nicko" "Janick"} is returned.
Combine Maps and Sets
Given the following map and set:
Write an expression which first adds the guitarists set with the key :solos
to the song map, and then retrieves the :solos set again from the map,
thereby removing the value "Janick" from the set.
Hint: Use the assoc and disj functions.
Test: The set {"Dave" "Adrian"} is returned.