Hi,
Continuing my little pet project (small program really) to learn Clojure, I
am now working on a function whose description would be something like:
"Returns a collection 'weaving' 2 collections (boundaries into
categories).
Boundaries must have one element less than categories.
For example, if categories is [:z1 :z2 :z3 :z4 :z5]
and boundaries is [120 150 165 180]
returns [:z1 120 :z2 150 :z3 165 :z4 180 :z5]"
Assume the precondition is enforced.
If categories has n elements, boundaries has n-1.
I have tried to come up with a good implementation. I actually came up with
two, one that is non recursive, and one that is recursive. But I'm not
fully satisfied. I have the feeling that it's possible to make the function
simpler, and more elegant but I only know a subset of Clojure and am surely
missing some good idioms that I could/should be using.
So once again, relying on feedback from experienced Clojurists to show me
the way :-)
Here is what I have so far:
1- the non recursive function - based on mapcat
(defn build-quantize-defs
[categories boundaries]
(conj (into [] (mapcat #(vector %1 %2) categories boundaries)) (last
categories)))
2 - the recursive function
(defn build-quantize-defs-recur [categories boundaries]
(let [c (first categories) b (first boundaries)]
(if (nil? b)
[c]
(into [] (concat [c] [b] (build-quantize-defs-recur (rest categories)
(rest boundaries)))))))
Both functions work (on my example at least).
One of the things I don't like, is my abusing (or the feeling that I am
abusing anyway) of this "into [] " idiom. I find myself constantly turning
things into vectors. That doesn't seem right and maybe I am using it in
places it's not needed. That's probably because I don't quite have a very
good idea of how collections work just yet.
Thanks for any feedback.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.