何时以及如何在 Clojure 中使用独立层次结构?
Clojure 用于创建临时关键字层次结构的系统对于大多数花过一些时间使用该语言的人来说都很熟悉。例如,该语言的大多数演示和演示都包含诸如此类的示例,
(derive ::child ::parent)
并且它们继续展示如何将其用于多方法调度。
在我见过的所有幻灯片和演示文稿中,他们都使用全局层次结构。但是可以通过使用 (derive h ::child ::parent) 将关键字关系放入独立的层次结构中,其中 h 是由 (make-hierarchy) 创建的。因此,有一些问题:
- 是否有关于何时有用或必要的指导原则?
- 有没有可以操纵层次结构的函数?
合并特别有用,所以我这样做:
(defn merge-h [& hierarchies]
(apply merge-with (cons #(merge-with clojure.set/union %1 %2) hierarchies))
但我想知道这样的函数是否已经存在于某处。
编辑:将“自定义”层次结构更改为“独立”层次结构,因为该术语更好地描述了这种动物。另外,我还做了一些研究,并在下面包含了我自己的答案。欢迎进一步评论。
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:
- Are there any guidelines on when this is useful or necessary?
- Are there any functions for manipulating hierarchies?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在过去的几天里,我深入研究了这个问题,我想我有一些答案:
使用独立的层次结构可以让相同的关键字拥有多个不同的层次结构。例如:
(def 篮球层次结构 (derive (make-hierarchy) :gretzky :jordan))
(def hockey-hierarchy (derive (make-hierarchy) :jordan :gretzky))
可以表明 :jordan 在篮球方面更高层次结构比 :gretzky 更重要,而在曲棍球层次结构中则相反。
另外:非命名空间关键字可以放入独立的层次结构中,但不能放入全局层次结构中。所以
会起作用,但对于全局层次结构,这些关键字必须位于命名空间中:
其中 nspace 是某个命名空间。 (有趣的是,这个命名空间实际上不必在任何地方声明。)
总之,当需要多个层次结构时,或者当需要在运行时广泛修改层次结构时,应该使用独立的层次结构。另外,如果我们想要裸关键字的层次结构,则需要独立的层次结构。
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
will work, but for the global hierarchy, these keywords would have to be in a namespace:
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.