Or if you want something that is agnostic about the structure of your input
map, try this:

(defn alter-map
  [in-map]
  (map (partial reduce merge)
       (partition
        (count (keys in-map))
        (apply interleave
               (map (fn [p]
                      (map #(hash-map (first p) %)
                           (seq (.split (fnext p) "\\s"))))
                    in-map)))))

; Usage:
(def data '( {:animal "dog cat bird" :sound "woof meow chirp"
:number-of-feet "4 4 2"}
                {:animal "horse" :sound "neeeee" :number-of-feet "4"}
                {:animal "wolf pig" :sound "howl oink":number-of-feet "4
4"}))
(pprint (map alter-map data))

;(({:number-of-feet "4", :sound "woof", :animal "dog"}
;  {:number-of-feet "4", :sound "meow", :animal "cat"}
;  {:number-of-feet "2", :sound "chirp", :animal "bird"})
; ({:number-of-feet "4", :sound "neeeee", :animal "horse"})
; ({:number-of-feet "4", :sound "howl", :animal "wolf"}
;  {:number-of-feet "4", :sound "oink", :animal "pig"}))

Of course, there's probably a better way to write that.

-Brendan

On Fri, Mar 5, 2010 at 4:36 PM, Tayssir John Gabbour <t...@pentaside.org>wrote:

> Hi Base,
>
> Perhaps something like the following:
>
> (defn normalize-creatures [creatures]
>  (letfn [(split [str] (seq (.split str "\\s")))]
>    (for [{:keys [animal sound number-of-feet]} creatures]
>      (map (fn [a s n]
>             {:animal a :sound s :number-of-feet n})
>           (split animal) (split sound) (split number-of-feet)))))
>
>
> ;; *foo* is the test data you mentioned
> (normalize-creatures *foo*)
> => (({:animal "dog"   :sound "woof"   :number-of-feet "4"}
>      {:animal "cat"   :sound "meow"   :number-of-feet "4"}
>     {:animal "bird"  :sound "chirp"  :number-of-feet "2"})
>     ({:animal "horse" :sound "neeeee" :number-of-feet "4"})
>     ({:animal "wolf"  :sound "howl"   :number-of-feet "4"}
>     {:animal "pig"   :sound "oink"   :number-of-feet "4"}))
>
>
> All the best,
> Tayssir
>
>
>
>
> On Mar 6, 1:07 am, Base <basselh...@gmail.com> wrote:
> > Hi all -
> >
> > I am trying to transofrm a data structure and am kind of stuck.
> >
> > I currently have a list of maps
> >
> > ( {:animal "dog cat bird" :sound "woof meow chirp" :number-of-feet "4
> > 4 2"}
> >   {:animal "horse" :sound "neeeee" :number-of-feet "4"}
> >   {:animal "wolf pig" :sound "howl oink":number-of-feet "4 4"} )
> >
> > and want to trasform it to:
> >
> > (
> >   ({:animal "dog" :sound "woof" :number-of-feet "4" }
> >    {:animal "cat" :sound "meow" :number-of-feet "4"}
> >    {:animal "bird" :sound "chirp" :number-of-feet "2"})
> >
> >   ({:animal "horse":sound "neeee" :number-of-feet "4"})
> >
> >   ({:animal "wolf" :sound "howl" :number-of-feet "4"}
> >    {:animal "pig" :sound "oink" :number-of-feet "4"})
> > )
> >
> > *spacing added for clarity*
> >
> > I have tried a few goofy attempts at this that were somewhat
> > successful. but what I don't like is that most of my attempts (which i
> > am kind of embarrassed to post) end up looking very un-clojuresque,
> > and much more imperative looking...and we all know there is a better,
> > more idiomatic way...
> >
> > any help is (as always) most appreciated.
> >
> > thanks
> >
> > base
>
> --
> 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<clojure%2bunsubscr...@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 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

Reply via email to