This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-02-21
Channels
- # announcements (7)
- # babashka (16)
- # beginners (174)
- # biff (7)
- # calva (20)
- # cider (3)
- # clerk (6)
- # cljsrn (4)
- # clojure (98)
- # clojure-europe (57)
- # clojure-italy (3)
- # clojure-nl (1)
- # clojure-portugal (1)
- # clojure-spec (15)
- # clojure-uk (7)
- # code-reviews (3)
- # cursive (23)
- # data-science (1)
- # datomic (26)
- # dev-tooling (2)
- # emacs (5)
- # figwheel-main (4)
- # fulcro (3)
- # honeysql (20)
- # hyperfiddle (20)
- # malli (8)
- # membrane (31)
- # nextjournal (5)
- # pathom (1)
- # polylith (20)
- # re-frame (14)
- # reitit (8)
- # releases (2)
- # shadow-cljs (50)
- # specter (2)
- # sql (22)
- # xtdb (5)
I have a list of named ::patterns
-- think of them as the [?e ?a ?v] patterns in the datalog :where
I want to make sure that every pattern is "joinable". This means there is at least one variable in subseuent patterns, which existed in the previous patterns.
So this is joinable:
(ensure-joinable
(s/conform ::patterns '[[:ea ?e ?a ?v]
[:ea ?e ?b ?c]]))
; ?e connects the second pattern with the first
But this would throw:
(try (ensure-joinable
(s/conform ::patterns '[[:ea ?e ?a ?v]
[:ea ?c ?b ?c]]))
(catch clojure.lang.ExceptionInfo e
(ex-data e)))
; no way to connect the second pattern
Here's my implementation:
(defn ensure-joinable [[first-pat & rest-pats :as named-pats]]
(doall (reduce
(fn [seen named-pat]
(let [vset (named-variables named-pat)
connected? (seq (cset/intersection seen vset))]
(if-not connected?
(throw (ex-info "Pattern is not joinable" {:patterns named-pats
:bad named-pat}))
(cset/union seen vset))))
(named-variables first-pat)
rest-pats)))
I'm using reduce
because I want to be a bit more friendly in the error message. Would you do it differently?