Should I implement the Seq interface in clojure

I have a data structure in Clojure, which is a collection of experimental results:

(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 deftype that implements this protocol. I am interested in writing functions that perform functions such as displaying a function for all results in a result set, or that add up for a set of results, basically doing the same thing as the built-in seq functions.

In Haskell, I would do this by executing the appropriate classes (like Functor) and then using functions like fmap or mfilter. So I reviewed this in Clojure and got some ideas about implementing the ISeq interface.

So is this a good idea? I canโ€™t find many resources for implementing ISeq, and Iโ€™m wondering what an idiomatic approach to this is.

+6
source share
2 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 (.seq) to map your ResultSet to a (possibly lazy) sequence. This clojure route is used for most (all?) Collections except the list (lists implement ISeq because the seq API is already a subset of the list API).

+5
source

I think I donโ€™t understand your question, but donโ€™t you just use a map to apply a function to every element of your result.

0
source

Source: https://habr.com/ru/post/896675/


All Articles