Last active
January 26, 2021 18:29
-
-
Save fogus/c4e69b657131ec5644bcf62ec79d7ef3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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