'keycollect-partition' looks sensible and idiomatic to me... perhaps juxt can come into play here? Of course, both versions will only work with keys as keywords and non-keys as non-keywords. If you use anything else other than a keyword for a key or a keyword for an item, both will break...

Jim


On 20/02/13 11:50, Stefan Kamphausen wrote:
Hi,

given a vector of the form


  [:key1 1 2 3 :key2 4 :key3 5 6 7]

I wand to create a map collecting the items behind each keyword as a vector 
like this:

  {:key1 [1 2 3]
   :key2 [4]
   :key3 [5 6 7]}

I have already written two functions which achieve this, but neither of them "feels 
good" and I am interested in more elegant and idiomatic solutions.

(defn keycollect-partition [coll]
   (->> coll
        (partition-by keyword?)      ; bundle at key
        (partition 2)                ; kw + its arg
        (map (fn [[[sec] arg]] [sec (vec arg)])) ; destruct the mess
        (into {})))                 ; make it a map

(defn keycollect-reduce [coll]
   (apply zipmap
          (reduce
           (fn [ac x]
             (if (keyword? x)
               [(conj (first ac) x) (conj (second ac) [])]
               (update-in ac [1 (dec (count (first ac)))] conj x))) [[] []]
               coll)))

Added complexity: My original vector does not yet contain keywords, but I 
construct them in amap  regexp-matching each item and creating a keyword from 
the first group of the match.

Any pointers or ideas appreciated.

Regards,
Stefan

--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to