Trie 类型应该扩展哪个协议?

MON*_*A43 2 types records clojure trie

我正在 Clojure 中编写 Trie 数据结构的实现,并认为最好使用 defrecord 来创建我自己的类型,该类型可以重载操作集合的函数。如何确定要扩展的协议名称,以便为我的新 Trie 类型实现 conj 和其他类似功能?

Mic*_*zyk 5

如果您想实现新的数据结构,则需要使用deftype而不是defrecord,因为后者为结果类型硬连线了映射行为的某种实现。

\n\n

至于clojure.core采集功能,大多数都是基于接口而不是协议。(然而,ClojureScript 确实使用协议。)发现哪些接口可能与新数据结构相关的最简单方法是检查已经存在的类似数据结构:

\n\n
;; all superclasses and interfaces of the class of {}, that is,\n;; clojure.lang.PersistentArrayMap\n(ancestors (class {}))\n\n;; interfaces only\n(filter #(.isInterface %) (ancestors (class {})))\n
Run Code Online (Sandbox Code Playgroud)\n\n

既然您计划实现一个特里树,我猜您想实现一个映射或集合。如果是这样,data.avl实现了所有相关接口(以及 ClojureScript 版本中的所有相关协议) \xe2\x80\x93 你可以看看源代码。

\n