In Java it's common to simply use a for loop and a get method to iterate through custom collections that don't implement Iterable.
for (int i=0; i<fooCollection.size(); i++) {There are patterns for doing something similar in Clojure; however, I much prefer to work with Sequences and functions that operate on Sequences. Shane Harvie and I added the following snippet to our code to quickly convert a custom collection to a seq.
Foo foo = fooCollection.get(i);
}
(defmacro obj->seq [obj count-method]Our first version only worked with a custom collection that had a .size() method; however, the second collection we ran into used a .length() method, so we created the above macro that allows you to specify the (size|length|count|whatever) method.
`(loop [result# []
i# 0]
(if (< i# (. ~obj ~count-method))
(recur (conj result# (.get ~obj i#)) (inc i#))
result#)))
The obj->seq function can be used similar to the following example.
(let [positions (obj->seq position-collection size)]I'm fairly new to Clojure, so please feel free to provide recommendations and feedback.
; do stuff with positions
)
You could do this without macros by implementing a collection-size function that selected an implementation based on the type passed in. Then use a conversion fn that calls collection-size. Advantages: (1) don't need macros, and (2) callers don't have to pass in the name of the count method.
ReplyDeleteIn 1.2, protocols provide a sexy way to deal with this, see the patch at http://www.assembla.com/spaces/clojure/tickets/289-add-internalreduce.
You can use lazy sequences to provide a more concise and performant solution:
ReplyDelete(defmacro obj->seq [obj count-method]
`(map #(.get ~obj %) (range (. ~obj ~count-method))))
Cool thanks Stu and Jürgen
ReplyDeleteYou might not need a macro, as there doesn't appear to be a need for syntactic extension.
ReplyDelete...building off of Jurgen's example....
(defn obj->seq [obj count-method]
(map #(.get obj %) (range (. obj count-method))))
This approach won't work because the symbol count-method will be resolved when calling obj->seq
ReplyDelete