我的问题很简单,但同时也很奇怪,我想创建一个避免求值的PersistentArrayMap,同时我想得到里面的值。最佳解决方案是什么?我是说
假设我有这样的定义:
(def queen "catherine the great")并想这样做(单引号):
'{:queen queen}确保输出为=> {:queen queen}
但是我希望做这样的事情,=> {:queen "catherine the great"}
我知道我能做到
(array-map :queen queen)但在我的例子中,我只想评估一些信息,因为我的地图更复杂,就像一个数据查询:
'{:find [(pull $ ?c [*])]
:with []
:in [$ ?queen-name]
:where [
[$ ?c :queen/name ?queen-name]
]
:args [queen-name]} 在这种情况下,我只想评估queen name。
我的问题是,有一种简单的方法可以做到吗?也许可以使用更新?
像这样的东西?
(assoc-in '{:find [(pull $ ?c [*])]
:with []
:in [$ ?queen-name]
:where [
[$ ?c :queen/name ?queen-name]
]
:args []} [:args] ["catherine the great"])发布于 2020-06-04 05:30:22
对于这两个示例,您都可以使用syntax-quote and unquote
user=> (def queen-name "catherine the great")
#'user/queen-name
user=> `{:queen ~queen-name}
{:queen "catherine the great"}
user> {:find '[(pull $ ?c [*])]
:with '[]
:in '[$ ?queen-name]
:where '[
[$ ?c :queen/name ?queen-name]
]
:args `[~queen-name]}
{:find [(pull $ ?c [*])],
:with [],
:in [$ ?queen-name],
:where [[$ ?c :queen/name ?queen-name]],
:args ["catherine the great"]} 发布于 2020-06-04 07:28:10
如果你想在一般情况下这样做,你可以使用tupelo.quote。
(ns demo.core
(:require [tupelo.quote :as q]))
; problem: free symbols a and b are fully-qualified using current ns
`[a b ~(+ 2 3)] => [demo.core/a
demo.core/b
5]
(q/tmpl-fn '[a b (insert (+ 2 3))]) => [a b 5]
(let [a 1 b 2]
(q/tmpl [a b (insert (+ 2 3))])) => [1 2 5]
(is= [1 [2 3 4] 5] (q/tmpl [1 (insert (t/thru 2 4)) 5]))
(is= [1 2 3 4 5] (q/tmpl [1 (splice (t/thru 2 4)) 5]))特别是对于Datomic,您可以使用use query inputs
(def some-name "John Lennon") ; parameter
;; query
(d/q '[:find ?release-name ; query pattern (quoted)
:in $ ?artist-name
:where [?artist :artist/name ?artist-name]
[?release :release/artists ?artist]
[?release :release/name ?release-name]]
db, some-name ; inputs (not quoted)
)有结果
#{["Power to the People"]
["Unfinished Music No. 2: Life With the Lions"]
["Live Peace in Toronto 1969"]
["Live Jam"]
...}https://stackoverflow.com/questions/62182650
复制相似问题