I have a list of maps and I need to know if one other map contains in this list:
List of maps:
[{:a 1} {:a 2} {:a 3}]
My map:
{:a 2}
Expected output:
true
I had tried to use contains but do not work. I come up with a solution but I don't think its a good one:
(map
(fn [x]
(if (= x {:a 1})
true
false))
[{:a 1} {:a 2} {:a 3}])
The result of this code I'm getting a list of true or false, and I will need to see if contains one true in this list.
So my question is, exist some way easier to verify if exist a map in a list of maps?
I think this question can be generalized to "how can I tell if a sequence contains a value?" some
is good for this:
(some pred coll)
It returns the first logical true value from pred
by invoking it with each item of coll
.
For example, the predicate can be a simple equality check:
user=> (some #(= % {:foo 1})
[{:foo 1} {:foo 2}])
true
It's also common to use sets as predicates:
One common idiom is to use a set as pred, for example this will return
:fred
if:fred
is in the sequence, otherwise nil:(some #{:fred} coll)