Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outer join in Clojure

Similar to this question: Inner-join in clojure

Is there a function for outer joins (left, right and full) performed on collections of maps in any of the Clojure libraries?

I guess it could be done by modifying the code of clojure.set/join but this seems as a common enough requirement, so it's worth to check if it already exists.

Something like this:

(def s1 #{{:a 1, :b 2, :c 3}
          {:a 2, :b 2}})

(def s2 #{{:a 2, :b 3, :c 5}
          {:a 3, :b 8}})


;=> (full-join s1 s2 {:a :a})
;
;   #{{:a 1, :b 2, :c 3}
;     {:a 2, :b 3, :c 5}
;     {:a 3, :b 8}}

And the appropriate functions for left and right outer join, i.e. including the entries where there is no value (or nil value) for the join key on the left, right or both sides.

like image 709
Goran Jovic Avatar asked Oct 22 '12 10:10

Goran Jovic


1 Answers

Sean Devlin's (of Full Disclojure fame) table-utils has the following join types:

  • inner-join
  • left-outer-join
  • right-outer-join
  • full-outer-join
  • natural-join
  • cross-join

It hasn't been updated in a while, but works in 1.3, 1.4 and 1.5. To make it work without any external dependencies:

  • replace fn-tuple with juxt
  • replace the whole (:use ) clause in the ns declaration with (require [clojure.set :refer [intersection union]])
  • add the function map-vals from below:

either

(defn map-vals
  [f coll]
  (into {} (map (fn [[k v]] {k (f v)}) coll)))

or for Clojure 1.5 and up

(defn map-vals
  [f coll]
  (reduce-kv (fn [acc k v] (assoc acc k (f v))) {} coll))

Usage of the library is join type, two collections (two sets of maps like the example above, or two sql resultsets) and at least one join fn. Since keywords are functions on maps, usually only the join keys will suffice:

=> (full-outer-join s1 s2 :a :a)
   ({:a 1, :c 3, :b 2}
    {:a 2, :c 5, :b 3}
    {:b 8, :a 3})

If I remember correctly Sean tried to get table-utils into contrib some time ago, but that never worked out. Too bad it never got it's own project (on github/clojars). Every now and then a question for a library like this pops up on Stackoverflow or the Clojure Google group.

Another option might be using the datalog library from datomic to query clojure data structures. Stuart Halloway has some examples in his gists.

like image 178
12 revs Avatar answered Nov 05 '22 12:11

12 revs