摘要远离Clojure中的数据结构实现细节 [英] Abstracting away from data structure implementation details in Clojure

查看:113
本文介绍了摘要远离Clojure中的数据结构实现细节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Clojure中开发了一个具有多个子结构的复杂数据结构。



我知道我会随着时间的推移扩展这个结构,并且有时可能想改变内部结构而不破坏数据结构的不同用户(例如I可能希望将向量更改为hashmap,为性能原因添加某种索引结构,或者合并Java类型)



我当前的想法是:




  • 使用各种访问器方法定义整体结构的协议

  • 创建一个小型函数库,数据结构(query-substructure-abc param1 param2)

  • 使用defrecord或deftype实现数据结构,协议方法定义为使用迷你库



    • 我认为这将工作,虽然我担心它开始看起来像很多glue代码。这也可能也反映了我更熟悉面向对象的方法。



      在Clojure中推荐的方法是什么?

      解决方案

      我认为 deftype 可能是要走的路,但我会通过访问器方法。相反,请查看 clojure.lang.ILookup clojure.lang.Associative ;这些是接口,如果你实现它们为你的类型,将让你使用 get / get-in code> assoc / assoc-in ,创建一个更多功能的解决方案(不仅可以更改底层实现,但也许还要使用基于Clojure标准集合库的函数来操作你的结构)。



      需要注意几件事:


      1. 您应该可以从 defrecord 开始,使用 get assoc & Co.与标准 defrecord 实现 ILookup 关联 IPersistentMap java.util.Map



        如果/当这些不再足够时,请查看的源代码, emit-defrecord (在Clojure的源中的 core_deftype.clj 中定义的私有函数)。


      2. deftype defrecord 目前为您定义任何工厂函数,但你应该自己做。


      3. 更概念上,复杂的操作当然是完美的适合协议功能建立在基础 get & Co。


      哦,看看 gvec.clj 在Clojure的源代码中使用 deftype 编写的一些严重的数据结构代码可能看起来像一个例子。这里的复杂性与你在问题中描述的不同,但是,它仍然是Clojure中定制数据结构编程的一些例子,目前可供公众使用(当然是优秀的代码)。

      当然,这正是我的直觉告诉我的。我不确定在这个阶段有成熟的习惯的方式,有什么与 deftype 实际上没有被释放和所有。 : - )


      I am developing a complex data structure in Clojure with multiple sub-structures.

      I know that I will want to extend this structure over time, and may at times want to change the internal structure without breaking different users of the data structure (for example I may want to change a vector into a hashmap, add some kind of indexing structure for performance reasons, or incorporate a Java type)

      My current thinking is:

      • Define a protocol for the overall structure with various accessor methods
      • Create a mini-library of functions that navigate the data structure e.g. (query-substructure-abc param1 param2)
      • Implement the data structure using defrecord or deftype, with the protocol methods defined to use the mini-library

      I think this will work, though I'm worried it is starting to look like rather a lot of "glue" code. Also it probably also reflects my greater familiarity with object-oriented approaches.

      What is the recommended way to do this in Clojure?

      解决方案

      I think that deftype might be the way to go, however I'd take a pass on the accessor methods. Instead, look into clojure.lang.ILookup and clojure.lang.Associative; these are interfaces which, if you implement them for your type, will let you use get / get-in and assoc / assoc-in, making for a far more versatile solution (not only will you be able to change the underlying implementation, but perhaps also to use functions built on top of Clojure's standard collections library to manipulate your structures).

      A couple of things to note:

      1. You should probably start with defrecord, using get, assoc & Co. with the standard defrecord implementations of ILookup, Associative, IPersistentMap and java.util.Map. You might be able to go a pretty long way with it.

        If/when these are no longer enough, have a look at the sources for emit-defrecord (a private function defined in core_deftype.clj in Clojure's sources). It's pretty complex, but it will give you an idea of what you may need to implement.

      2. Neither deftype nor defrecord currently define any factory functions for you, but you should probably do it yourself. Sanity checking goes inside those functions (and/or the corresponding tests).

      3. The more conceptually complex operations are of course a perfect fit for protocol functions built on the foundation of get & Co.

      Oh, and have a look at gvec.clj in Clojure's sources for an example of what some serious data structure code written using deftype might look like. The complexity here is of a different kind from what you describe in the question, but still, it's one of the few examples of custom data structure programming in Clojure currently available for public consumption (and it is of course excellent quality code).

      Of course this is just what my intuition tells me at this time. I'm not sure that there is much in the way of established idioms at this stage, what with deftype not actually having been released and all. :-)

      这篇关于摘要远离Clojure中的数据结构实现细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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