Search code examples
dictionaryclojure

How to apply a function to each element of a list or vector in Clojure


I see that the map function exists in Clojure, but I don't understand how to refer to each element in the list. Not sure if it is possible. In Ruby, I would write something like this:

list_of_numbers = [1,2,3]
list_of_numbers.map {|num| num * 2}

can I do something like that with the map function in Clojure?


Solution

  • (def nums [1 2 3])
    (def doubles (mapv #(* % 2) nums))   ; or just `map`
    (println doubles)
    
    => [2 4 6]
    

    For a good start, see: