Clojure's system for creating an ad hoc hierarchy of keywords is familiar to most people who have spent a bit of time with the language. For example, most demos and presentations of the language include examples such as
(derive ::child ::parent)
and they go on to show how this can be used for multi-method dispatch.
In all of the slides and presentations that I've seen, they use the global hierarchy. But it is possible to put keyword relationships in independent hierarchies, by using (derive h ::child ::parent), where h is created by (make-hierarchy). Some questions, therefore:
Merging is particularly useful, so I do this:
(defn merge-h [& hierarchies]
(apply merge-with (cons #(merge-with clojure.set/union %1 %2) hierarchies))
But I was wondering if such functions already exist somewhere.
EDIT: Changed "custom" hierarchy to "independent" hierarchy, since that term better describes this animal. Also, I've done some research and included my own answer below. Further comments are welcome.
I've delved into this problem over the last few days, and I think I have some answers:
Using independent hierarchies allows you to have several different hierarchies for the same keywords. So for instance:
(def basketball-hierarchy (derive (make-hierarchy) :gretzky :jordan))
(def hockey-hierarchy (derive (make-hierarchy) :jordan :gretzky))
Could indicate that :jordan is higher in the basketball hierarchy than :gretzky, whereas the reverse is true in the hockey hierarchy.
Also: non-namespaced keywords can be put into independent hierarchies, but not into the global hierarchy. So
(derive (make-hierarchy) :a :b)
will work, but for the global hierarchy, these keywords would have to be in a namespace:
(derive :nspace/a :nspace/b)
where nspace is some namespace. (This namespace does not actually have to be declared anywhere, interestingly.)
In summary, then, independent hierarchies should be used when more than one hierarchy is needed, or when the hierarchy needs to be modified extensively during run time. Also, if we want a hierarchy for naked keywords, an independent hierarchy is required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With