2
votes

I have a Clojure Lazy Sequence:

{
    {:keyOne 123, :keyTwo "TestVal"}
    {:keyOne 456, :keyTwo "Value2"}
    {:keyOne 789, :keyTwo "TestVal"}
}

I want to get the maps which have a specific value for a given key, e.g. I want all maps which have the value "TestVal" as the :keyTwo value, so I'd expect the first and third element in my result.

I assume I should be able to solve this using filter, but I've looked through all examples I could find and they never use such a nested structure.

1

1 Answers

5
votes
{{:keyOne 123, :keyTwo "TestVal"}
 {:keyOne 456, :keyTwo "Value2"}
 {:keyOne 789, :keyTwo "TestVal"}}

In clojure, this expression doesn't make sense, this isn't the lazy sequence of maps. To answer your question adequately,I think input data is like as below:

(def input '({:keyOne 123, :keyTwo "TestVal"}
             {:keyOne 456, :keyTwo "Value2"}
             {:keyOne 789, :keyTwo "TestVal"}))

We can make the expression for your purpose like this:

(filter (fn [m] (= "TestVal" (:keyTwo m))) input)

It doesn't care whether the input sequence is lazy or not-lazy(eager).