Fork me on GitHub
#meander
<
2022-05-18
>
jgdavey01:05:55

What’s the best way to represent repetition where one sub-element in the repeated clause is the same, while the other can differ? Example:

;; would match, because of repeated :foo
  [:or
   [:= :foo 123]
   [:= :foo 456]]

  ;; would not match
  [:or
   [:= :foo 123]
   [:= :BAR 456]]
I’m trying to do a rewrite of the first example above that would result in [:one-of :foo [123 456]] I would have thought that this would work:
(m/rewrite
  [:or
   [:= :foo 123]
   [:= :foo 456]]
  [:or . [:= ?a !b] ...]
  [:one-of ?a [!b ...]])
But results in error “Zero or more patterns may not have references to unbound logic variables.”

jgdavey13:05:57

Looks like this does it:

(m/rewrite
    [:or
     [:= :foo 123]
     [:= :foo 456]]
    [:or [:= ?a !b] . [:= ?a !b] ...]
    [:one-of ?a [!b ...]])

👍 2