在clojure中应该如何使用独立的层次结构? [英] When and how should independent hierarchies be used in clojure?

查看:127
本文介绍了在clojure中应该如何使用独立的层次结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Clojure的用于创建关键字的临时层次结构的系统对于花费了一段时间的语言的大多数人来说是熟悉的。例如,语言的大多数演示和演示包括例如

 (deriv :: child :: parent)

,他们继续展示如何将其用于多方法分派。



在我看过的所有幻灯片和演示文稿中,他们使用全局层次结构。但是可以通过使用(derived h :: child :: parent),其中h由(make-hierarchy)创建,在独立层次中放置关键字关系。因此,有一些问题:




  • 这是否有用或有必要吗?

  • 有操作层次的任何功能吗?



合并特别有用,所以我这样做:

 (defn merge-h [& hierarchyies] 
(apply merge-with(cons#(merge-with clojure.set / union%1%2)hierarchy))



但我想知道这些函数是否已经存在于某处。



编辑:将自定义层次结构更改为独立层次结构,因为该术语更好地描述了这个动物,另外,我已经做了一些研究并包括我自己的答案,欢迎进一步的评论。 / p>

解决方案

我在过去几天里遇到了这个问题,我想我有一些答案:


  1. 独立层次结构中使用的函数与在全局层次结构中使用相同的函数时具有不同的语义。Doing(deriv kwd1 kwd2)产生副作用 - 另一方面,(派生层级kwd1 kwd2)是有效的 - 函数返回一个新的层次结构,而不是修改var。

  2. 使用独立的层次结构允许您为相同的关键字具有几个不同的层次结构。例如:



    (def篮球层次结构(派生(层次结构):gretzky:jordan))



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


:jordan在篮球层次结构中高于:gretzky,反之则在曲棍球层次结构中。



此外:非命名关键字可以放入独立的层次结构,但不进入全局层次结构。

 (derived(make-hierarchy):a:b)
pre>

将工作,但对于全局层次结构,这些关键字必须在命名空间中:

 (派生:nspace / a:nspace / b)

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



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


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.

解决方案

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.

这篇关于在clojure中应该如何使用独立的层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆