This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-23
Channels
- # announcements (7)
- # babashka (11)
- # babashka-sci-dev (1)
- # beginners (8)
- # biff (1)
- # calva (8)
- # chlorine-clover (3)
- # cider (9)
- # clj-kondo (16)
- # cljdoc (9)
- # clojure (79)
- # clojure-australia (1)
- # clojure-dev (5)
- # clojurescript (24)
- # data-science (3)
- # datomic (2)
- # emacs (54)
- # fulcro (1)
- # graalvm (1)
- # jobs (5)
- # lsp (25)
- # malli (7)
- # meander (10)
- # off-topic (44)
- # other-languages (8)
- # portal (10)
- # remote-jobs (1)
- # sci (1)
- # shadow-cljs (6)
- # vim (4)
Hi! I’m using meander to do this:
(m/search inferred-schemata
(m/and
(m/scan
{:create-table ?table
:with-columns (m/scan [?field _type])})
(m/let [?idx (condp re-find (->snake-str ?field)
#"uuid$" :exact
#"name$" :nocase
#"^alias" :nocase
nil)]))
(let [table (->snake-str ?table)
field (->snake-str ?field)
index (format "idx_%s_%s" table field)
tail (when (= ?idx :nocase) " COLLATE NOCASE")]
[(format "CREATE INDEX %s ON %s (%s)%s;" index table field tail)]))
However, I only want to match when (some? ?let)
— how can I do that for an m/let introduced variable?I’m able to hack around it fine:
(->>
(m/search inferred-schemata
(m/and
(m/scan
{:create-table ?table
:with-columns (m/scan [?field _type])})
(m/let [?idx (condp re-find (->snake-str ?field)
#"uuid$" :exact
#"name$" :nocase
#"^alias" :nocase
nil)]))
(when ?idx
(let [table (->snake-str ?table)
field (->snake-str ?field)
index (format "idx_%s_%s" table field)
tail (when (= ?idx :nocase) " COLLATE NOCASE")]
[(format "CREATE INDEX %s ON %s (%s)%s;" index table field tail)])))
(remove nil?))
but I’d love to be able to just write it in meander(defn test []
(m/search {:create-table "foo"
:with-columns {"foo" "bar"}}
(m/with [%index (m/or (m/let [?idx :exact, ?tail ""]
(m/re #".*uuid"))
(m/let [?idx :nocase, ?tail " COLLATE NOCASE"]
(m/re #"^alias.*|.*name$")))
%table (m/app ->snake-str ?table)
%field (m/app ->snake-str ?field %index)]
{:create-table %table
:with-columns (m/scan [%field _type])})
(let [index (format "idx_%s_%s" ?table ?field)]
[(format "CREATE INDEX %s ON %s (%s)%s;" index ?table ?field ?tail)])))
(do
(def ->snake-str
(constantly "fooname"))
(test))
;; =>
(["CREATE INDEX idx_fooname_fooname ON fooname (fooname) COLLATE NOCASE;"])
(do
(def ->snake-str
(constantly "aliasfoo"))
(test))
;; =>
(["CREATE INDEX idx_aliasfoo_aliasfoo ON aliasfoo (aliasfoo) COLLATE NOCASE;"])
(do
(def ->snake-str
(constantly "foouuid"))
(test))
;; =>
(["CREATE INDEX idx_foouuid_foouuid ON foouuid (foouuid);"])
In the two m/let
cases we can bind ?tail
since
tail (when (= ?idx :nocase) " COLLATE NOCASE")
from the code you pasted.