Last modified on 6 March 2014, at 14:36

Clojure Programming/Examples/API Examples/Mapping Operators

mapEdit

user=> (map + [1 2 3 4] [1 2 3 4])
(2 4 6 8)

reduceEdit

user=> (reduce * [2 3 4])
24
; sum the odd numbers to 100
;; Not so beautiful code, see next example for a better approach
(reduce #(+ %1 (if (= 1 (rem %2 2)) %2 0)) (range 100)) 

; sum the odd numbers to 100 (Cleaner version)
(reduce + (filter odd? (range 100)))

applyEdit

user=> (apply str [1 2])
"12"

(defn factorial [n] 
  (apply * (range 2 (inc n))))