Write a function which, given a key and map, returns true
if the map contains an entry with that key and its value is nil
.
Solution I came across:
#(nil? (get %2 % true))
Can someone please explain the use of true
in
(get %2 % true) ?
Thank you!
that's a default value that will be returned in the case of absence of the key
;; key exists
(get {:a 1} :a 2)
#=> 1
;; key doesn't exist (default value is returned)
(get {:a 1} :b 2)
#=> 2
;; key exists and it's value is nil
(get {:a nil} :a 2)
#=> nil
;; key doesn't exist, nil is returned
(get {:a 1} :b)
#=> nil
some docs could be found here
https://clojuredocs.org/clojure.core/get
so the idea is that (get {:a 1} :b)
will always return nil
because key doesn't exist. In this case (nil? (get {:a 1} :b))
will return true, which is not what we want. That's why adding this default value is necessary. So nil
will be returned only in the case when the actual value is nil
.
The value true is not special here. 85 would work just as well: any value other than nil fixes the problem.