外部加入Clojure [英] Outer join in Clojure

查看:100
本文介绍了外部加入Clojure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此问题类似:内在加入clojure

在任何Clojure库中的映射集合上是否有外连接(左,右和全)的函数?

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

I猜测它可以通过修改 clojure.set / join 的代码来完成,但这似乎是一个常见的要求,所以值得检查它是否已经存在。

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.

这样的东西:

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

左右外连接,即包括左侧,右侧或两侧的连接键没有值(或 nil value)的条目。

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.

推荐答案

Sean Devlin的( Full Disclojure 名声) table-utils 具有以下加入类型:

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

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

它并未在一段时间内更新,但在1.3,1.4和1.5中有效。要使其无需任何外部依赖性即可工作:

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:


  • fn-tuple 替换为 juxt

  • 将ns声明中的整个(:use) (require [clojure.set:refer [intersection union]])

  • 从下面添加函数map-vals:

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

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

或Clojure 1.5及以上

or for Clojure 1.5 and up

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

库的使用是连接类型,两个集合(两个映射类似上面的示例或两个sql结果集)和至少一个连接fn。由于关键字是地图上的函数,通常只有连接键就足够了:

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



如果我记得正确Sean试图获取表 - -utils进入contrib一段时间前,但那从来没有成功。太糟糕了,它从来没有它自己的项目(在github / clojars)。

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.

另一个选项可能是使用datalog库从数据库查询clojure数据结构。 Stuart Halloway拥有一些 例子在他的精神。

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

这篇关于外部加入Clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆