Fork me on GitHub
#core-logic
<
2021-11-10
>
tschady14:11:30

I’m trying to compare elements inside an lvar which is a hashmap (in this case, having a unique value for the :a key). I’ve managed to do it with project but I’ve read this is to be avoided. Is there a better way to do this? Any advice on style? m will have over 100 entries eventually, with more complex comparison between adjacent vars.

(def m [{:a 1 :b "foo"}
        {:a 1 :b "bar"}
        {:a 3 :b "baz"}])

(let [vars (repeatedly 2 lvar)]
  (run* [q]
    (everyg #(membero % m) vars)
    (project [vars] (distincto (mapv :a vars)))
    (== q vars)))

paulocuneo14:11:21

https://github.com/clojure/core.logic/blob/master/src/main/clojure/clojure/core/logic.clj#L2587 can be use to extract keys from a map, dunno the performance

(run* [q]
  (fresh [ma a ma' a']
    (membero ma m)
    (membero ma' m)
    (featurec ma {:a a})
    (featurec ma' {:a a'})
    (distincto [a a'])
    (== [a a'] q)))

Ben Sless14:11:26

Ninja'd by less than a minute featurec can bind keys and values in a map

tschady15:11:12

I found featurec but hadn’t adapted my thinking to that a a' yet. thanks!