I had some fun with this and added assert-args:
(defmacro assert-args [fnname pred msg & pred-msg-pairs]
`(when-not ~pred
(throw (IllegalArgumentException. ~(str fnname " requires " msg))))
(when (seq pred-msg-pairs)
(list* `assert-args fnname pred-msg-pairs)))
(def ascending compare)
(def descending #(compare %2 %1))
(defn compare-by [& key-cmp-pairs]
(assert-args compare-by
(even? (count key-cmp-pairs)) "even number of args (keyword,
comparator)"
(every? #(or (keyword? %) (fn? %)) key-cmp-pairs) "all args to be
keywords or functions")
(fn [x y]
(loop [[k cmp & more] key-cmp-pairs]
(let [result (cmp (k x) (k y))]
(if (and (zero? result) more)
(recur more)
result)))))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en