Search code examples
clojure

How do I check if a value in Clojure hash-map exists and equals nil?


When programming in Clojure, I get the same fair answer for both questions:

(nil? (:potato {}))
=> true
(nil? (:potato {:potato nil}))
=> true

This sometimes leads to confusion. Is there a function that would help me discover the difference?

Something like:

(property-nil? {} :potato)
=> false
(property-nil? {:potato nil} :potato)
=> true

Solution

  • You can use a default value with the map lookup, supplying any non-nil value as the default. In this example I am using the string "anything":

    (nil? (:potato {:potato nil} "anything"))
    ; => true
    
    (nil? (:potato {:fred nil} "anything"))
    ; => false