On Oct 7, 2010, at 2:29 AM, Stefan Rohlfing wrote:
> Dear Clojure Group,
>
> Following an Enlive tutorial I wanted to implement a function 'd-map'
> that takes an arbitrary number of [:key (list of values)] parameters
> like this:
>
> (d-map [:headline ["this" "is" "me"] ]
> [:points [1 2 3] ]
> [:comments [10 20 30] ])
>
>
> and returns a collection of [:key value] vectors where each value is
> grouped with its respective key:
>
> [ [:headline "this"] [:headline "is"] [:headline "me"]
> [:points 1] [:points 2] [:points 3]
> [:comments 10] [:comments 20] [:comments 30] ]
>
>
> I could only come up with the following implementation for 'd-map'.
> Although it works it just does not 'feel' elegant of even very
> functional:
>
> (defn d-map [& kfps]
> (let [keys (map first kfps)
> fns (map second kfps)]
> (loop [keys keys fns fns res []]
> (if (seq keys)
> (recur (rest keys) (rest fns)
> (into res (map (fn [x] [(first keys) x]) (first
> fns))))
> res))))
If you treat a map as a sequence it will yield the vectors you are handling
explicitly:
(map identity {:a [1 2 3] :b [4 5 6]}) => ([:a [1 2 3]] [:b [4 5 6]])
So it might be more convenient to pass in a map and do something like this:
(defn d-map [m]
(apply concat
(map (fn [[key val-list]]
(map (fn [val] [key val])
val-list))
m)))
(d-map {:headline ["this" "is" "me"]
:points [1 2 3]
:comments [10 20 30]})
([:headline "this"] [:headline "is"] [:headline "me"] [:points 1] [:points 2]
[:points 3] [:comments 10] [:comments 20] [:comments 30])
Have all good days,
David Sletten
--
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