Alex Robbins
There are some extra-wonderful functions/features that you might not know about. You can get by without these functions, but with them you'll be happier.
(map {:a 1 :b 2} [:a :c])
;; (1 nil)
(map #{:a} [:a :b])
;; (:a nil)
([:a :b] 0)
;; :a
(def input [1 {:key "value" :key2 "value2"}])
(let [[a] input]
a)
;; 1
(let [[_ a] input]
a)
;; {:key "value" :key2 "value"}
(let [[_ {a :key}] input]
a)
;; "value"
(let [[_ {:keys [key key2] :as whole-map}] input]
[key key2 whole-map])
;; ["value" "value2" {:key "value" :key2 "value2"}]
(map zero? [0 1])
;; (true false)
(map pos? [0 1])
;; (false true)
(not= 0 1)
;; true
(compare 0 1)
;; -1
(compare "b" "a")
;; 1
(compare :a :a)
;; 0
(for [x (range)
:let [x2 (* x x)]
:when (odd? x)
:while (< x2 100)]
[x x2])
;; ([1 1] [3 9] [5 25] [7 49] [9 81])
(mapcat (fn [x] [x (* x x)]) [0 1 2])
;; (0 0 1 1 2 4)
(mapcat expand-variant-spellings ["Alexander" "John"])
["Alexander" "Aleksander" "Alex" "John" "Jon"]
(zipmap [:a :b :c] (range))
;; {:c 2, :b 1, :a 0}
(pmap inc (range 100000000))
;; same as (map inc (range 100000000)), but computed on all cores
(-> :a f1 f2 (f3 :b) <=> (f3 (f2 (f1 :a)) :b)
(->> :a f1 f2 (f3 :b) <=> (f3 :b (f2 (f1 :a)))
Think of it as data being threaded through a pipeline.
(reductions + [1 1 1 1 1])
;; (1 2 3 4 5)
(reductions conj [] [1 2 3])
;; ([] [1] [1 2] [1 2 3])
(condp = (:type {:type :person :name "Alex"})
:person (println "Person found")
:canine (println "Dog found"))
(group-by :suspicious [{:name "Alex" :suspicious false}
{:name "Chris" :suspicious true}
{:name "Lee" :suspicious false}])
;; {false [{:suspicious false, :name "Alex"}
{:suspicious false, :name "Lee"}],
true [{:suspicious true, :name "Chris"}]}
(frequencies [:a :a :b :a :c])
;; {:a 3 :b 1 :c 1}
(get-in {:a {:b 1}} [:a :b])
;; 1
(update-in {:a {:b 1}} [:a :b] + 3)
;; {:a {:b 4}}
(map (constantly 1) [1 2 3 4])
;; (1 1 1 1)
(def nil-patched-+ (fnil + 1))
(nil-patched-+ nil 2)
;; 3
(deftest test-my-func
(is (= 2 (+ 1 1))))
(deftest test-my-func
(are [input output]
(= (* input input) output)
0 0
1 1
2 4
3 9
4 16))
(declare db-call summary)
(defn summary []
(+ (db-call 1) (db-call 2)))
(deftest test-summary
(with-redefs [db-call (constantly 2)]
(is (= (summary) 4))))
Alex Robbins
Factual is hiring!