It is a very confusing comparison between map and apply.

; data preparation
(def data  [[1,2,3],[1,3,4],[1,5,6]])

The famous function map means to call function provided to each element in collection once at a time.

(map println data)

;[1 2 3]
;[1 3 4]
;[1 5 6]
;(nil nil nil)

There are 3 elements in data, map calls println for each element.

Whereas appy just call function once, but extract all elements from collection before calling.

(apply println data)

;[1 2 3] [1 3 4] [1 5 6]
;nil

Here only has 1 line of printing.

Some more experiments:

(apply map println data)

;1 1 1
;2 3 5
;3 4 6
;(nil nil nil)

Obviously, apply extracts elements from collection.

(eval `(map println ~@data))
;this is equivalent to 
(map println [1,2,3] [1,3,4] [1,5,6])

;1 1 1
;2 3 5
;3 4 6
;(nil nil nil)


Published

26 April 2015

Category

development

Tags