In clojure records, how to map array of records to vector?
If I have an vector of records that implement a protocol, and want to map the return开发者_开发知识库 value of a method of each of those records to another vector, is there a clean way to do that? I can just use map with an anonymous function wrapping the call to the method, but that seems a bit clunky.
Edited:
Well, actually, there's nothing to this. You just use the method as the function in first argument to the call to map.
(map mymethod myrecords)
Due to an unrelated mistake, that wasn't working for me and I thought I had to do...
(map #(mymethod %) myrecords)
...which is what I thought was clunky. So the question is invalid.
Methods on records work with map like every other method:
(defprotocol Foo
(foo [this]))
(defrecord Bar [bar]
Foo
(foo [this] bar)
(map foo [(Bar. 1) (Bar. 2)])
=> (1 2)
精彩评论