为什么 cond 看起来 'cache' 是一个内部绑定?

Why does cond appear to 'cache' an internal binding?

我正在使用此函数构建 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' 正在关闭该值并重新使用它。谢谢。

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