为什么cond似乎会缓存'内部约束?

时间:2016-04-26 14:17:31

标签: clojure datomic

我正在使用此函数构建Datomic事务,然后我将其映射到输入关键字列表:

(defn build-enum-transaction [inp]
  (cond
    (.contains (namespace (first inp)) "region")
       [:db/add #db/id[:db.part/region] :db/ident (first inp)]
    (.contains (namespace (first inp)) "sector")
       [:db/add #db/id[:db.part/sector] :db/ident (first inp)]
    (.contains (namespace (first inp)) "specialism")
       [:db/add #db/id[:db.part/specialism] :db/ident (first inp)]))

(defn build-all-enum-transactions [inp]
  (vec (map build-enum-transaction inp)))

build-all-enum-transactions的输入数据是:

([:region/EU]
 [:region/UK]
 [:region/NAFTA]
 [:sector/NON-CYCLICALS]
 [:sector/FINANCIALS]
 [:specialism/INSURANCE]
 [:specialism/VAT])

我得到以下结果:

[[:db/add #db/id[:db.part/region -1000289] :db/ident :region/EU]
 [:db/add #db/id[:db.part/region -1000289] :db/ident :region/UK]
 [:db/add #db/id[:db.part/region -1000289] :db/ident :region/NAFTA]
 [:db/add #db/id[:db.part/sector -1000290] :db/ident :sector/NON-CYCLICALS]
 [:db/add #db/id[:db.part/sector -1000290] :db/ident :sector/FINANCIALS]
 [:db/add #db/id[:db.part/specialism -1000291] :db/ident :specialism/INSURANCE]
 [:db/add #db/id[:db.part/specialism -1000291] :db/ident :specialism/VAT]]

正如您所看到的,:db.part / 应为每个产生一个增量数字,但它只对每个' cond'条款。为什么是这样?似乎' cond'正在关闭该值并重新使用它。谢谢。

1 个答案:

答案 0 :(得分:2)

您应该使用d/tempid在运行时创建一个tempid。 #db/id是一个读取器宏,在读取程序时会扩展为tempid,i。即编译时间。