0
votes

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?

1
Turning your tuples into a map i'd consider a good idea - giving things names is very powerful and you even had an answer for your question.cfrick

1 Answers

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

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

Please see this list of documentation.