On Fri, 5 Mar 2010 16:07:55 -0800 (PST)
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"})
> )

How about this?

(use '[clojure.contrib.str-utils2 :only [split]])
(defn cook [animals]
  (map (fn [{:keys [animal sound number-of-feet]}]
         (map #(hash-map :animal %1 :sound %2 :number-of-feet %3)
              (split animal #"\s")
              (split sound #"\s")
              (split number-of-feet #"\s")))
       animals))

which gives:

(({: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"}))

-- 
Mike Meyer <m...@mired.org>             http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

-- 
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