何时以及如何在 Clojure 中使用独立层次结构?

发布于 2024-09-05 05:36:12 字数 652 浏览 11 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-09-12 05:36:12

在过去的几天里,我深入研究了这个问题,我想我有一些答案:

  1. 在独立层次结构上使用的函数与在全局层次结构上使用相同的函数时具有不同的语义。执行 (derive kwd1 kwd2) 会产生副作用——全局层次结构中的一个条目。另一方面, (derive Hierarchy kwd1 kwd2) 是函数式的——该函数返回一个新的层次结构,而不是修改 var。
  2. 使用独立的层次结构可以让相同的关键字拥有多个不同的层次结构。例如:

    (def 篮球层次结构 (derive (make-hierarchy) :gretzky :jordan))

    (def hockey-hierarchy (derive (make-hierarchy) :jordan :gretzky))

可以表明 :jordan 在篮球方面更高层次结构比 :gretzky 更重要,而在曲棍球层次结构中则相反。

另外:非命名空间关键字可以放入独立的层次结构中,但不能放入全局层次结构中。所以

(derive (make-hierarchy) :a :b)

会起作用,但对于全局层次结构,这些关键字必须位于命名空间中:

(derive :nspace/a :nspace/b)

其中 nspace 是某个命名空间。 (有趣的是,这个命名空间实际上不必在任何地方声明。)

总之,当需要多个层次结构时,或者当需要在运行时广泛修改层次结构时,应该使用独立的层次结构。另外,如果我们想要裸关键字的层次结构,则需要独立的层次结构。

I've delved into this problem over the last few days, and I think I have some answers:

  1. Functions used on independent hierarchies have different semantics from when the same functions are used on the global hierarchy. Doing (derive kwd1 kwd2) produces a side-effect -- an entry in the global hierarchy. On the other hand, (derive hierarchy kwd1 kwd2) is functional -- the function returns a new hierarchy, instead of modifying a var.
  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文