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.

Jdv
  • 962
  • 10
  • 34

1 Answers1

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).

naoki fujita
  • 689
  • 1
  • 9
  • 13
  • 3
    `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. – amalloy Oct 30 '17 at 18:46
  • Your comment is exact. It is meaningless to treat eager sequence as lazy seq. I will modify my answer more practical and accurate. – naoki fujita Oct 30 '17 at 23:51