Hi, I've just read the "On the importance of recognizing and using maps" post and it made wonder about the best way for identifying maps. Obviously, when the situation permit it, we better use some kind of label to identify them. But sometimes, it's preferable to test for multiple keys and I'm not sure what's the best way to do this. We can use contains? but it takes only one key. I've made a modification to contains? so that it can take many keys as shown in the patch below.
P.S.: I've still not mailed a contributor agreement, tried to send it two months ago, but it came back. I had totally forgotten about postage stamps! It's astonishing how the web can make you used to get stuff for free ;-) I'll try again this week.... with stamps this time! --- src/clj/clojure/core.clj (revision 1301) +++ src/clj/clojure/core.clj (working copy) @@ -900,7 +900,16 @@ vectors and Java arrays, this tests if the numeric key is within the range of indexes. 'contains?' operates constant or logarithmic time; it will not perform a linear search for a value. See also 'some'." - [coll key] (. clojure.lang.RT (contains coll key))) + ([coll key] (. clojure.lang.RT (contains coll key))) + ([coll key & keys] + (loop [keys (cons key keys) acc true] + (if keys + (let [key (first keys) + res (and acc (. clojure.lang.RT (contains coll key)))] + (if (and res (seq? keys)) + (recur (next keys) res) + res)) + acc)))) (defn get "Returns the value mapped to key, not-found or nil if key not present." --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---