Skip to content

Instantly share code, notes, and snippets.

@fogus
Last active January 26, 2021 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogus/c4e69b657131ec5644bcf62ec79d7ef3 to your computer and use it in GitHub Desktop.
Save fogus/c4e69b657131ec5644bcf62ec79d7ef3 to your computer and use it in GitHub Desktop.
(defn add [& {:keys [:a :b]}]
(+ a b))
(add :a 1 :b 2)
;;=> 3
(add :a)
;;= No value supplied for key: :a
(add {:a 1 :b 2})
;;=> 3
;; WHAT SHOULD HAPPEN?
(add {:a 1 :b 2} {:a 10 :b 20})
;;=> WHAT HAPPENS HERE?
(add :a 1 {:b 2})
;;=> 3
;;;; PARTIAL
(def add1 (partial add :a 1))
(add1 :b 2)
;;=> 3
(add1 {:b 2})
;;=> 3
(def add2 (partial add :b 2))
(add2 :a 1)
;;=> 3
(add2 {:a 1})
;;=> 3
;;;; OVERRIDE
(add2 {:a 1 :b 20})
;;=> 21
(add2 {:b 2})
;;= NPE because (+ nil b)
;;;; with pre-args
(defn frob [a b & {:keys [c d]}]
[a b c d])
(frob 1 2 :c 3 :d 4)
;;=> [1 2 3 4]
(frob 1 2 {:c 3 :d 4})
;;=> [1 2 3 4]
(frob 1 2 :c 3 {:d 4})
;;=> [1 2 3 4]
((partial frob 1 2 :c 3) :d 4)
;;=> [1 2 3 4]
((partial frob 1 2 :c 3) {:d 4})
;;=> [1 2 3 4]
((partial frob 1 2 :c 3 :d 4) {:d 40})
;;=> [1 2 3 40]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment