Thanks Sean! I've come up with this variation:
(def ascending compare) (def descending #(compare %2 %1)) (defn compare-by [& key-cmp-pairs] (fn [x y] (loop [[k cmp & more] key-cmp-pairs] {:pre [(keyword? k), (fn? cmp), (even? (count more))]} (let [result (cmp (k x) (k y))] (if (and (zero? result) more) (recur more) result))))) I'm not really sure what I think of preconditions yet, but I'm enjoying playing with the idea. Alex On Tue, Nov 30, 2010 at 10:07 AM, Sean Devlin <francoisdev...@gmail.com>wrote: > If you like assert args, you should check out pre & post conditions > for your functions. They're built in to Clojure since 1.1 > > The official docs: > > http://clojure.org/special_forms#Special%20Forms--%28fn%20name?%20%28[params*%20]%20condition-map?%20exprs*%29+%29 > > Video Tutorial: > http://vimeo.com/8399758 > > On Nov 29, 10:35 pm, Alex Baranosky <alexander.barano...@gmail.com> > wrote: > > 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 clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com<clojure%2bunsubscr...@googlegroups.com> > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en