2

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 Answer 1

5
{{: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).

Sign up to request clarification or add additional context in comments.

2 Comments

map identity is very strange. There is no need to force the input to be lazy: anything that works for lazy sequences should work for eager sequences, including the code you have written.
Your comment is exact. It is meaningless to treat eager sequence as lazy seq. I will modify my answer more practical and accurate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.