This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-09-10
Channels
- # announcements (16)
- # aws (1)
- # babashka (2)
- # beginners (33)
- # biff (2)
- # clj-kondo (13)
- # cljs-dev (1)
- # cljsrn (3)
- # clojars (6)
- # clojure (198)
- # clojure-australia (3)
- # clojure-europe (41)
- # clojure-france (3)
- # clojure-nl (2)
- # clojure-spec (7)
- # clojure-uk (12)
- # clojurescript (57)
- # clojureverse-ops (1)
- # code-reviews (3)
- # community-development (2)
- # conjure (10)
- # data-science (1)
- # datomic (15)
- # depstar (2)
- # docker (2)
- # etaoin (1)
- # events (1)
- # exercism (5)
- # fulcro (23)
- # helix (23)
- # introduce-yourself (4)
- # jobs (6)
- # kaocha (1)
- # lsp (11)
- # meander (107)
- # off-topic (8)
- # pathom (3)
- # polylith (33)
- # re-frame (23)
- # reagent (7)
- # reitit (28)
- # remote-jobs (3)
- # sci (1)
- # shadow-cljs (2)
- # specter (5)
- # sql (38)
- # tools-deps (72)
- # web-security (9)
- # xtdb (32)
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) ?
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
(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
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.