user=> (def partner {:laurel :hardy, :hardy :laurel, :chaplin nil})
#'user/partnerNow you can look up, say, Laurel's partner:
user=> (get partner :laurel)
:hardyIn our map, Chaplin does not have a partner, so you get this:
user=> (get partner :chaplin)
nilOf course - we made the value nil when we created the map. However, if we look up someone who is not in the map, we also get nil:
user=> (get partner :abbott)
nilThe problem (A nil key) today is to write the code that will check for the case where the key really is in the map but the value is nil - - as opposed to the case where the key is not in the map.
We need the function contains?, which asks whether a key is in a map:
user=> (contains? partner :abbott)
false
user=> (contains? partner :chaplin)
trueAnd we also need the useful fact that nil equals nil. Therefore the code we want looks like this:
(fn [k m]
(and
(contains? m k)
(= (get m k) nil)))
No comments:
Post a Comment