开发者

Should I implement the Seq interface in clojure

开发者 https://www.devze.com 2023-04-03 08:37 出处:网络
I have a data structure in Clojure which represents a set of results from an experiment: (defprotocol ResultSet

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"))

And I have a deftype which imp开发者_Go百科lements 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.

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.

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.


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).


I think I'm misunderstanding your question but wouldn't you just use map to apply a function to each element in your result.

0

精彩评论

暂无评论...
验证码 换一张
取 消