Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
variable-arity arg weirdness
(def person1 {:fname "John" :mname "Q" :lname "Doe"})
(def person2 {:fname "Jane" :mname "P" :lname "Doe"})
(defn fname-compare [p1 p2]
(do
(println "Comparing fname")
(compare (:fname p1) (:fname p2))))
(defn lname-compare [p1 p2]
(do
(println "Comparing lname")
(compare (:lname p1) (:lname p2))))
(defn mname-compare [p1 p2]
(do
(println "Comparing mname")
(compare (:mname p1) (:mname p2))))
(defn compose-comparators [& comparators]
(println (type comparators)) ;; let's see what type comparators is
(fn [x y]
(let [result
(->> comparators
(map #(% x y))
(filter #(not (zero? %)))
first)]
(if (nil? result)
0
result))))
user=> ((compose-comparators fname-compare lname-compare mname-compare) person1 person2)
clojure.lang.ArraySeq
Comparing fname
14
user=> (def x ((compose-comparators fname-compare lname-compare mname-compare) person1 person2))
clojure.lang.PersistentVector$ChunkedSeq
Comparing fname
Comparing lname
Comparing mname
#'user/x
user=> x
14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.