我正在尝试使用 Clojure core.logic 从地图中提取数据。此代码按我的意愿执行:
(run 10 [q]
(fresh [?id ?type ?name]
(membero ?type [:t2 :t1])
(== q [?id ?name])
(membero {:id ?id :type ?type :name ?name}
[
{:id 1 :type :t1 :name "one"}
{:id 2 :type :t2 :name "two"}
])))
=> ([2 "two"] [1 "one"])
但是,如果我向地图添加一些其他元素,则它无法匹配:
(run 10 [q]
(fresh [?id ?type ?name]
(membero ?type [:t2 :t1])
(== q [?id ?name])
(membero {:id ?id :type ?type :name ?name}
[
{:id 1 :type :t1 :name "one" :other1 :o1}
{:id 2 :type :t2 :name "two" :other2 :o2}
])))
我明白为什么matcho
地图不再一样了。我的问题是,我怎样才能改变它以使其再次工作?如何使其仅匹配地图中的某些键,或者如何使其匹配地图可能具有的任何随机键?
编辑:
我让它工作patial-map
(感谢https://github.com/clojure/core.logic/pull/10)
(run 10 [q]
(fresh [?id ?type ?name]
(membero ?type [:t2 :t1])
(== q [?id ?name])
(membero (partial-map {:id ?id :type ?type :name ?name} )
[
{:id 1 :type :t1 :name "one" :other1 :o2}
{:id 2 :type :t2 :name "two" :other2 :o1}
])))
但是我发现了一个featurec
应该用来代替的注释partial-map
。我的新问题:如何将其更改为使用featurec
?