# The Map Function In Clojure

The next member of the Big Three Higher Order Functions is “map“.

I don’t think “map” is a good name for it because 1. There is a data structure named “map” (which has different names in different languages) and 2. I don’t think it is the most descriptive name for it. “apply-to-every” or “apply-to-all” might be better.

The “map” function takes a function and one or more collections, and applies the function to each member in the collection. In Clojure, if you send multiple collections, they will be concatenated into one output collection.

I think the reason it is called “map” is that you are using a function to map a collection onto another collection.

Clojure also has “mapv” which does the same thing as “map” except that it returns a vector, and “mapcat” which takes functions that take collections as arguments. The functions sent to map and mapv take single arguments (if you send one collection) or multiple args, but not collections.

```; a function that prints out what it's doing to better understand "map"
user> (defn inc-p [x]
(println "x is " x " (inc x) is " (inc x))
(inc x))

#'user/inc-p
user> (map inc-p [1 2 3 4])
x is  1  (inc x) is  2
x is  2  (inc x) is  3
x is  3  (inc x) is  4
x is  4  (inc x) is  5
(2 3 4 5)
user> (mapv inc-p [1 2 3 4])
x is  1  (inc x) is  2
x is  2  (inc x) is  3
x is  3  (inc x) is  4
x is  4  (inc x) is  5
[2 3 4 5]

;; if you send multiple collections, your function has to be able to take multiple args
user> (map inc-p [1 2 3 4] [2 3 4 5])
Error printing return value (ArityException) at clojure.lang.AFn/throwArity (AFn.java:429).
Wrong number of args (2) passed to: user/inc-p
user> (map + [1 2 3 4] [2 3 4 5])
(3 5 7 9)
user> (mapc + [1 2 3 4] [2 3 4 5])
Syntax error compiling at (on-demand-repl:localhost:42447(clj)*:112:7).
Unable to resolve symbol: mapc in this context
user> (mapv + [1 2 3 4] [2 3 4 5])
[3 5 7 9]
user> (mapv + [1 2 3 4] [2 3 4 ])
[3 5 7]
user>

user> (mapcat rest [1 2 3 4])
Execution error (IllegalArgumentException) at user/eval5576 (form-init2164788925227159491.clj:74).
Don't know how to create ISeq from: java.lang.Long
user> (mapcat rest [[1 2 3 4]])
(2 3 4)
user> (mapcat rest [[1 2 3 4] [2 3 4 5]])
(2 3 4 3 4 5)
user>

```

You’re welcome.