Hey guys! In this episode, we are going to learn about maps.

Lets jump right in.

“Don’t get lost when creating a map”

(= {:a 1 :b 2} (hash-map :a 1 __ __))

:b 2

“A value must be supplied for each key”

(= {:a 1} (hash-map :a __))

1

“The size is the number of entries”

(= __ (count {:a 1 :b 2}))

2

Count will count the number of entries in a map, :a will be an entry and :b will be an entry. It does not count both the keys and values.

“You can look up the value for a given key”

(= __ (get {:a 1 :b 2} :b))

2

So looking up the value of b, which would be 2.

“Maps can be used as functions to do lookups”

(= __ ({:a 1 :b 2} :a))

1

The value would be 1!

“And so can keywords”

(= __ (:a {:a 1 :b 2}))

1

Also 1.

“But map keys need not be keywords”

(= __ ({2010 “Vancouver” 2014 “Sochi” 2018 “PyeongChang”} 2014))

“Sochi”

They can be numbers as well!

“You may not be able to find an entry for a key”

(= __ (get {:a 1 :b 2} :c))

nil

“But you can provide your own default”

(= __ (get {:a 1 :b 2} :c :key-not-found))

:key-not-found

“You can find out if a key is present”

(= __ (contains? {:a nil :b nil} :b))

true

“Or if it is missing”

(= __ (contains? {:a nil :b nil} :c))

false

The keyword :c is missing.

“Maps are immutable, but you can create a new and improved version”

(= {1 “January” 2 __} (assoc {1 “January”} 2 “February”))

“February”

“You can also create a new version with an entry removed”

(= {__ __} (dissoc {1 “January” 2 “February”} 2))

{1 “January”}

“Often you will need to get the keys, but the order is undependable”

(= (list __ __ __)

(sort (keys { 2014 “Sochi” 2018 “PyeongChang” 2010 “Vancouver”})))

2010 2014 2018

“You can get the values in a similar way”

(= (list __ __ __)

(sort (vals {2010 “Vancouver” 2014 “Sochi” 2018 “PyeongChang”}))))

“PyeongChang” “Sochi” “Vancouver”