Suppose I have this vector of maps:
[{:title "Title1" :id 18347125}
{:title "Title2" :id 18347123}
{:title "Title3" :id 18341121}]
And I wish to select the map with :id 18347125, how would I do this?
I've tried
(for [map maps
:when (= (:id map) id)]
map)
This feels a bit ugly and returns a sequence of length one, and I want to return just the map.
IMHO, there are several ways to solve your problem, and the definitely idiomatic way is in the realm of taste. This is my solution where I simply translated "to select maps whose :id
is 1834715
" into Clojure.
user> (def xs [{:title "Title1" :id 18347125}
{:title "Title2" :id 18347123}
{:title "Title3" :id 18341121}])
#'user/xs
user> (filter (comp #{18347125} :id) xs)
({:title "Title1", :id 18347125})
The :id
keyword is a function that looks up itself in a collection passed to it. The set #{18347125}
is also a function that tests if a value passed to it equals 18347125
. Using a Clojure set as a predicate function allows for a succinct idiom.
I'm not sure if it's the simplest way to write it, but I think this is more clear about your intentions:
(->> maps
(filter #(= (:id %) id))
first)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With