Search code examples
clojure

Clojure filter sequence of maps for a value in a list


Trying to filter a sequence of maps that have a value for a key in a list of values.

Trying to do something like this

=> (filter (fn [foo]
           #(some(:x foo) ["0000", "2222"])) 
           [{ :y "y" :x "0000"} {:y "y" :z "z" :x "1111"} {:y "y" :z "z" :x "2222"}])

; I want to return this
=> [{:y "y" :z "z" :x "1111"}]

edit: fixed typo


Solution

  • This produces the desired output:

    (filter #(not (#{"0000", "2222"} (:x %)))
            [{:y "y" :x "0000"}
             {:y "y" :z "z" :x "1111"}
             {:y "y" :z "z" :x "2222"}])
    => ({:y "y", :z "z", :x "1111"})
    

    This solution is using a set of those two strings, and testing :x of each input map for set membership.

    I think you have a typo and some other issues in your filter predicate example. I fixed the "0000" typo in this example to get the desired output.