Map Reduce - sorokod/Clojure-Notes GitHub Wiki


; ## map
(map inc [1 2 3])          ; (2 3 4)
(map inc [1 2 3] '(1 2 3)) ; (1 4 9) - multiple colletion parameters

; ## using map with a collection of functions
(def sum #(reduce + %))
(def avg #(/ (sum %) (count %)))
(defn stats
	[numbers]
	(map #(% numbers) [sum count avg]))

(stats [1 2 3])            ; (6 3 2)

; ## using map to retrive keyword values from maps
(def data [{:a 1 :b "1"} {:a 2 :b "2"} {:a 3 :b "3"}])
(map :b data)             ; ("1" "2" "3")

; ## reduce
(reduce + [1 2 3 4])       ; 10
(reduce + 15 [1 2 3 4])    ; 25 - optional initial value: