Aayush Lamichhane
Aayush Lamichhane

Reputation: 43

How do I filter a list of vectors in Clojure?

I am new to Clojure and learning the properties of various data structures in Clojure. Here, I have a list of vectors as follows:

(["1" "Christiano Ronaldo" "Portugal" "35"] 
 ["2" "Lionel Messi" "Argentina" "32"] 
 ["3" "Zinedine Zidane" "France" "47"])

where the first element of each vector is the id. How do I filter out single vectors from the list based on the id? For eg., id = 1 should return

["1" "Christiano Ronaldo" "Portugal" "35"]

I tried doing the same on a nested-map:

(def footballers 
    [
        {:id 1 :name "Christiano Ronaldo" :country "Portugal" :age 35}
        {:id 2 :name "Lionel Messi" :country "Argentina" :age 32}
        {:id 3 :name "Zinedine Zidane" :country "France" :age 47}
    ]
)

and was successful using the filter function

(filter #(= (:id %) 1) footballers)

Result:

({:id 1, :name "Christiano Ronaldo", :country "Portugal", :age 35})

How do I do the same in a list of vectors using filter function?

Upvotes: 0

Views: 454

Answers (1)

Alan Thompson
Alan Thompson

Reputation: 29958

(filterv #(= "1" (first %)) footballers)  ; or `filter`

   ;=> [["1" "Christiano Ronaldo" "Portugal" "35"]]  ; vector containing 1 vector

Please see this list of documentation.

Upvotes: 2

Related Questions