(defn add-up-apply [ & numbers ] (apply + numbers)) (add-up-apple 1 2 3 4) (defn add-up-reduce [ & numbers ] (reduce + 0 numbers)) (add-up-reduce 1 2 3 4) ;; reader macros ;; #( ... % ...) is short for (fn [_] ( ... _ ... )) ;; % is short for %1 ;; let takes a vector of pairs (let [ x expression1 y expression2] ( ... x ... y ... )) ;; (if condition true-expr false-expr ) ;; (when condition expr1 expr2 expr3 ) -> (if condition (do expr1 expr2 expr3) ) ;; (do expr1 expr2 expr3 ) executes three expressions, returns last one ;; (cond (cond1) (expr1) (cond2) (expr2) :default (expr3) ) (mod 31 3 ) (defn print-fizz-buzz [ numbers ] (map (fn [n] (cond (and (= 0 (mod n 3)) (= 0 (mod n 5))) 'fizz-buzz (= 0 (mod n 3)) 'fizz (= 0 (mod n 5)) 'buzz :default n ) ) numbers ) ) (print-fizz-buzz (range 100) ) (defn print-fizz-buzz [ numbers ] (let [fb (fn [n] (cond (and (= 0 (mod n 3)) (= 0 (mod n 5))) 'fizz-buzz (= 0 (mod n 3)) 'fizz (= 0 (mod n 5)) 'buzz :default n ) )] (if (empty? numbers) () (cons (fb (first numbers)) (print-fizz-buzz (rest numbers))) ) ) ) (print-fizz-buzz '(1 2 3)) ;; (loop [x expr1 y expr2] ( ... (recur (f x) (g y )))) ;; (doseq [x coll] (... x ...)) ;; :keyword ;; (def h { :a 1 :b 2} ) map structure ;; (h :a) but also (:a h) ;; 'literal - could also use as key in map (def h { :a 1 'b 2 } ) (h :a) (h 'b) (:a h) ('b h) ;; iSeq interface for all sequences / collections ;; can do first, last, count, rest, etc (first h) ; [ :a 1 ] ;; list -> list? etc (conj h [ :c 3 ]) (cons [ :d 4 ] h) ;; (1 2 3) fails because it tries to treat 1 as a fn, use '(1 2 3) or [1 2 3] ;; vector -> get, nth, peek, vector?, assoc, pop, replace, etc ( [1 2 3] 1) ;; map -> assoc, dissoc, merge, merge-with (uses fn to merge values), keys, vals (keys h) (vals h)