Hi!

If you'd like to use relational structures, take a look at  
clojure.set. There's a couple of functions which let you do relational  
algebra (project, select, rename, plus some other things like index).  
Clojure represents relations as sets of maps:

(def data #{{:id 0 :name "Fred"    :age 30}
             {:id 1 :name "Wilma"   :age 26}
             {:id 2 :name "Bam-bam" :age  2}
             {:id 3 :name "Dino"    :age  3}})

(def age-index (clojure.set/index data [:age]))

(defn name-by-age [a]
   (map :name (age-index {:age a})))


Kind regards,
Achim


Am 09.08.2009 um 08:27 schrieb Chad Harrington:

> Hi all,
> I am learning Clojure and would like to see if there is a better/ 
> more concise/faster/more idiomatic/etc. way to create the age-index  
> below.  My version seems awfully roundabout. The basic concept is a  
> toy database table stored as a hashmap.  Each row has a row-id and  
> and a vector of data [name, age].  I then create an index on the age  
> column.  The index should yield a row-id for the given age value.   
> My code below works as designed, but I'd like to see how the age- 
> index could be improved.
>
> Thanks for your insights.
>
> Chad
> ------------ code begin ----------------
> (use '[clojure.contrib.seq-utils :only (flatten)])
>
> (def data {
>            0 ["Fred" 30],
>            1 ["Wilma" 26],
>            2 ["Bam-bam" 2],
>            3 ["Dino" 3]})
>
> (def age-index
>   (apply sorted-map
>          (flatten (map #(list
>                          (nth (val %) 1)
>                          (key %))
>                        data))))
>
> (defn name-by-age [age]
>   (first (data (age-index age))))
>
> (println (name-by-age 26))
> ------------------ code end -------------------
>
> Chad Harrington
> chad.harring...@gmail.com
>
> >


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