clojurelighttable

Clojure - get data inside vector of vectors


I have a vector of vectors that contains some strings and ints:

(def data [
["a" "title" "b" 1]
["c" "title" "d" 1]
["e" "title" "f" 2]
["g" "title" "h" 1]
])

I'm trying to iterate through the vector and return(?) any rows that contain a certain string e.g. "a". I tried implementing things like this:

(defn get-row [data]
  (for [d [data]
        :when (= (get-in d[0]) "a")] d
  ))

I'm quite new to Clojure, but I believe this is saying: For every element (vector) in 'data', if that vector contains "a", return it?

I know get-in needs 2 parameters, that part is where I'm unsure of what to do.

I have looked at answers like this and this but I don't really understand how they work. From what I can gather they're converting the vector to a map and doing the operations on that instead?


Solution

  • (filter #(some #{"a"} %) data)
    

    It's a bit strange seeing the set #{"a"} but it works as a predicate function for some. Adding more entries to the set would be like a logical OR for it, i.e.

    (filter #(some #{"a" "c"} %) data)
    => (["a" "title" "b" 1] ["c" "title" "d" 1])