Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

I'd like to define a record type that implements ISeq and ISeqable, but I want it to work with functions like map while still outputting the original record type. The record type will "contain" a sequence, but also some other data that I want to stay associated even though I called map.

So, for example:

`
(defrecord Person [readingList name])

(def p (Person. '("book 1", "book 2") "Jane"))

(map #(capitalize %) p)
; should produce (Person. '("Book 1", "Book 2") "Jane"))
`

I realize that I could handle mapping the book list outside the record, but for my purposes I want to treat the Person record as a sequence that happens to have a bit of data that follows it as it is processed.

I'm having a hard time understanding if this is possible. I haven't found a good place that documents Clojure's common individual protocols, and what it requires to implement a specific one.

I thought maybe it'd be possible to do this if I could define cons, but it doesn't seem like cons is a protocol function in ISeq or ISeqable since it doesn't take the record itself as its first argument.

2 Answers

+1 vote
by

I'm not sure what are you trying to map over here. If you meant to work with the readingList then you should write
(update p :readingtlist (partial map capitalize))

which will return a Person.

I haven't found a good place that documents Clojure's common individual protocols, and what it requires to implement a specific one.

There isn't much documentation for this because it is atypical to do so. Clojure advocates usage of the built-in data structures to model your domain. Even creating records is often left until later.

0 votes
by

There is no way to control the concrete type of sequences created by sequence functions in the core api, so I think what you're asking for is probably not possible.

I would urge you instead to look into using the built-in data structures, possibly annotated with metadata, as an alternative to creating your own type - you probably don't need that.

...