6

我正在尝试使用 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

4

1 回答 1

6

回答我自己的问题:

这是工作代码:

(run 10 [q] 
   (fresh [?id ?type ?name ?pm] 
          (membero ?type [:t2 :t1])
          (== q [?id ?name])
          (featurec ?pm  {:id ?id :type ?type :name ?name} )
          (membero ?pm
                   [
                    {:id 1 :type :t1 :name "one" :other1 :o2}
                    {:id 2 :type :t2 :name "two" :other2 :o1 :another :ao2}
                    {:id 3 :type :t3 :name "three" :other2 :o1 :another :ao1}
                    ]))))

这段代码允许我从地图中提取值,但只关注真正重要的键。其余的键值对将被忽略。

于 2013-01-04T17:18:00.853 回答