Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner-join in clojure

Tags:

clojure

Lets say I have

(def test-left 
                            [{:name "Sean" :age 27} 
                             {:name "Ross" :age 27} 
                             {:name "Brian" :age 22}])

and

(def test-right 
                        [{:owner "Sean" :item "Beer"} 
                         {:owner "Sean" :item "Pizza"} 
                         {:owner "Ross" :item "Computer"} 
                         {:owner "Matt" :item "Bike"}])

When I do a (clojure.set/join test-left test-right) I get a Cartesian product. 12 rows. What I want is Inner-join on :name of left = :owner of right so that I get only those result which have matching rows in the right side

So I get

              [{:name "Sean" :age 27  :item “Beer” } 
               {:name "Sean" :age 27  :item “Pizza” }
               {:name "Ross" :age 27  :item “Computer”}]

Any Ideas. Similarly Would like to achieve left and right outer joins but that is separete question. I am learning clojure so please bear with me

like image 591
Ash Avatar asked Jul 14 '11 14:07

Ash


1 Answers

Got it . My Bad

1:24 main-hta=> (clojure.set/join test-left test-right {:name :owner})

gets

#{{:item "Pizza", :owner "Sean", :name "Sean", :age 27} {:item "Beer", :owner "Sean", :name "Sean", :age 27} {:item "Computer", :owner "Ross", :name "Ross", :ag
e 27}}
like image 146
Ash Avatar answered Sep 17 '22 12:09

Ash