我正在研究一些Clojure代码,其中有一个像这样的实体树:
foo1
+-- bar1
| +-- baz1
| +-- baz2
+-- bar2
+-- baz3
foo2
+-- bar3
+-- baz4
如果我的荒谬的ASCII艺术没有意义,我有一个 foos 列表,每个都可以有零个或多个 bar ,每个都可以有零个或多个 bazes 。
我要做的是生成一个哈希映射,其中键是 baz ID,值是 bar ID;即上图将是:
{"baz1" "bar1", "baz2" "bar1", "baz3" "bar2", "baz4" "bar3"}
我的数据结构如下所示:
(def foos [
{:id "foo1" :bars [
{:id "bar1" :bazes [
{:id "baz1"}
{:id "baz2"}
]}
{:id "bar2" :bazes [
{:id "baz3"}
]}
]}
{:id "foo2" :bars [
{:id "bar3" :bazes [
{:id "baz4"}
]}
]}
])
以下是构建baz-to-bar地图的代码:
(defn- baz-to-bar [foos]
(let [b2b-list (flatten (for [f foos] (flatten (for [bar (:bars c)] (flatten (for [baz (:bazes bar)] [(:id baz) (:id bar)]))))))
b2b-map (if (not (empty? b2b-list)) (apply hash-map b2b-list))]
(if b2b-map [:b2b (for [baz (keys b2b-map)] (entry-tag baz (b2b-map baz)))])))
它有效,但非常迟钝。
有人可以在Clojure中建议一种更优雅,更有希望的惯用方法吗?
答案 0 :(得分:6)
(into {} (for [foo foos
{bar-id :id :as bar} (:bars foo)
{baz-id :id} (:bazes bar)]
{baz-id bar-id}))