简体   繁体   中英

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?

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

=> [2 4 6]

For a good start, see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM