This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-07
Channels
- # announcements (38)
- # asami (14)
- # beginners (35)
- # biff (3)
- # calva (29)
- # cider (20)
- # clj-kondo (7)
- # cljdoc (38)
- # clojure (64)
- # clojure-art (2)
- # clojure-australia (1)
- # clojure-dev (6)
- # clojure-europe (30)
- # clojure-nl (4)
- # clojure-spec (4)
- # clojure-uk (9)
- # clojured (3)
- # clojurescript (87)
- # cursive (17)
- # datahike (2)
- # datomic (10)
- # defnpodcast (2)
- # emacs (2)
- # events (1)
- # fulcro (25)
- # gratitude (1)
- # introduce-yourself (1)
- # jobs-discuss (21)
- # lsp (103)
- # malli (41)
- # meander (8)
- # minecraft (3)
- # missionary (3)
- # nextjournal (20)
- # off-topic (10)
- # pedestal (1)
- # polylith (15)
- # portal (6)
- # releases (2)
- # ring (1)
- # ring-swagger (2)
- # sci (4)
- # shadow-cljs (5)
- # spacemacs (3)
- # sql (11)
- # xtdb (3)
What’s an idiomatic way to express “all elements of this vector match this pattern”? For sets and maps, there’s the built-in map-of
and set-of
, but I don’t see something like seq-of
or vec-of
. It’s trivial to implement with defsyntax
, but feels like I’m missing something more fundamental.
Example:
(m/defsyntax vec-of
"Pattern matching and substitution operator.
When used as a pattern matching operator, matches a vector where all
the entries have keys which match `pattern`.
When used as a pattern substitution operator, constructs a vector where
all entries are constructed with `pattern`."
[pattern]
(cond
(m/match-syntax? &env)
`(m/with [%coll# (m/or [~pattern & %coll#]
'[])]
%coll#)
(m/subst-syntax? &env)
`[~pattern ...]
:else
&form))
(m/rewrite [:a :b]
(vec-of (m/and (m/keyword _) !x))
(vec-of !x)
?a :wrong) ;; => [:a :b]
[<pattern> ...]
Next issue: How can I specify that a logic variable repeats? That is, repetition of :a
keyword in the following example:
(m/rewrite [:a :a]
[(m/and (m/keyword ?n) !x) ...]
[!x ...]
?a :wrong)
(This doesn’t compile because of unbound logic var error)