Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I implement the Seq interface in 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"))

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.

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.

like image 362
Alex jg Avatar asked Sep 06 '11 00:09

Alex jg


1 Answers

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

like image 154
Joost Diepenmaat Avatar answered Sep 20 '22 22:09

Joost Diepenmaat