Fork me on GitHub
#specter
<
2021-09-10
>
johanatan19:09:13

hi, given two (nested) maps how could one a) verify that the same key structure exists in both and b) run some predicate (fn [v1 v2] ... ) that would compare the corresponding values at each key and short-circuit if any return false (returning false when short circuited and true only if entire walk proceeds to exhaustion) ?

johanatan19:09:12

(apologies if this has been asked before or is a basic question)

Joshua Suskalo19:09:48

Sounds like you should just do a select on both structures and use clojure.math.combinatorics/cartesian-product to make a list of all the pairs of items and then use (every? the-pred product) to get your value

Joshua Suskalo19:09:23

(require '[clojure.math.combinatorics :as combo]) ;; requires the org.clojure/math.combinatorics contrib library

(defn satisfies
  [pred & seqs]
  (every? pred (apply combo/cartesian-product seqs)))

(let [structure-1 {1 {:a :blah} 2 {:a :blah}}
      structure-2 [{:b :blah} {:b :blah}]]
  (satisfies = (select [MAP-VALS :a] structure-1)
             (select [ALL :b] structure-2))))
;; => true

Joshua Suskalo20:09:18

You could do additional validation like ensuring that both selects return at least one value, or an equal number of values, or whatever, based on your needs.