What is the idiomatic way of checking if a key on a map has a value? For example if we have:
=> (def seq-of-maps [{:foo 1 :bar "hi"} {:foo 0 :bar "baz"}])
To find out all the maps with :foo == 0, I like:
=> (filter (comp zero? :foo) seq-of-maps)
({:foo 0, :bar "baz"})
But if I want to find all the maps with :bar == “hi”, the best that I can think of is:
=> (filter #(= (:bar %) "hi") seq-of-maps)
({:foo 1, :bar "hi"})
which I don’t find very readable. Is there a better/more idiomatic way of doing it?
I personally like refactoring this kind of thing to use a clearly named higher order function:
Downside is that it gives you an extra function definition to manage and maintain, but I think the elegance / code readability is worth it. This approach can also be very efficient from a performance perspective if you re-use the predicate many times.