我在Clojure中有一个数据结构,代表了一组实验结果:
(defprotocol ResultSet
(columns [rs] "return a collection of the columns in the resultset, represented by keywords")
(rows [rs] [rs column-keys] "returns a seq of the rows in the resultset, order and column specified as keywords by column-keys. with a single argument returns rows with all columns present"))
我有一个deftype
实现了这个协议。我想编写一些函数,比如在结果集中映射一个函数,或者对结果集进行折叠,基本上做与内置的序列函数相同的事情。
在Haskell中,我会通过实现相关的类型类(例如Functor),然后使用像fmap或mfilter这样的函数来完成。因此,我研究了在Clojure中如何实现这一点,最终想到了实现ISeq接口的一些想法。
那么,这是一个好主意吗?我找不到很多关于实现ISeq的资源,我想知道这种做法的惯用方式是什么。