问题描述
我在Clojure中有一个数据结构,它表示一组实验的结果:
I have a data structure in Clojure which represents a set of results from an experiment:
(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
,它实现了这个协议。我感兴趣的是编写函数,这些函数将函数映射到结果集中的所有结果,或者折叠结果集,基本上做与内置seq函数相同的事情。
And I have a deftype
which implements this protocol. I am interested in writing functions which do things like map a function over all the results in the resultset, or which fold over the result set, basically doing the same things as the built in seq functions.
在Haskell中,我将通过实现相关类型类(如Functor)然后使用fmap或mfilter等函数来实现。所以我在Clojure中做了这件事,结束了关于实现ISeq接口的一些想法。
In Haskell I would do this by implementing the relevant typeclasses (eg Functor) and then using functions like fmap or mfilter. So I looked into doing this in Clojure and wound up with some ideas about implementing the ISeq interface.
那么,这是个好主意吗?我找不到很多关于实现ISeq的资源,我想知道这是什么惯用的方法。
So, is this a good idea? I can't find many resources about implementing ISeq and I am wondering what the idiomatic approach to this is.
推荐答案
我可以告诉,最好的实现这样的东西不是实现ISeq,而是clojure.lang.Seqable;换句话说,提供(.seq)的实现,以将ResultSet映射到(可能是延迟的)序列。这是路由clojure用于大多数(所有?)集合,除了list(列表实现ISeq,因为seq API已经是列表API的一个子集)。
As far as I can tell, the "best" way to implement something like this is not to implement ISeq, but clojure.lang.Seqable; in other words, provide an implementation of (.seq) to map your ResultSet to a (possibly lazy) sequence. That is the route clojure uses for most (all?) collections except list (lists implement ISeq, because the seq API is already a subset of the list API).
这篇关于我应该在clojure中实现Seq接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!