This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-08-05
Channels
- # all-the-channels (1)
- # announcements (3)
- # asami (13)
- # beginners (227)
- # calva (2)
- # chlorine-clover (8)
- # cider (7)
- # clj-kondo (4)
- # cljs-dev (21)
- # cljsrn (8)
- # clojure (64)
- # clojure-europe (39)
- # clojure-france (2)
- # clojure-italy (3)
- # clojure-losangeles (1)
- # clojure-nl (20)
- # clojure-uk (8)
- # clojurescript (24)
- # conjure (12)
- # datalog (9)
- # datomic (24)
- # emacs (8)
- # figwheel-main (1)
- # fulcro (15)
- # jobs-discuss (1)
- # malli (5)
- # meander (3)
- # off-topic (26)
- # re-frame (15)
- # reagent (10)
- # reitit (1)
- # reveal (1)
- # sci (15)
- # shadow-cljs (25)
- # spacemacs (7)
- # sql (3)
- # xtdb (1)
I needed a simple template system. I think this fits the bill for both simple and easy:
(m/rewrite {:keys #{::name ::state}
:template ["here is some data for" ::name
{:name ::name
:state ::state}]
:values {::name "Mark"
::state "California"}}
{:keys ?keys
:values ?values
:template (m/$ ?ctx (m/pred (partial contains? ?keys) ?key))}
(m/cata {:keys ?keys, :values ?values
:template (m/app ?ctx (m/app ?values ?key))})
{:template ?result} ?result)
Another tool you can use for this (not Meander) is clojure.walk/prewalk-replace
:
(let [bindings {::name "Mark"
::state "California"}
template ["here is some data for"
::name
{:name ::name
:state ::state}]]
(clojure.walk/prewalk-replace bindings template))
;; =>
["here is some data for" "Mark" {:name "Mark", :state "California"}]
Of course, if you don’t want something as blunt as prewalk-replace
you can accomplish the same thing with Meander and your own custom rules for more control:
(let [bindings {::name "Mark"
::state "California"}
template ["here is some data for"
::name
{:name ::name
:state ::state}]]
(m/rewrite [template bindings]
[[!xs ...] ?bindings]
[(m/cata [!xs ?bindings]) ...]
[{?k ?v & ?rest} ?bindings]
{(m/cata [?k ?bindings]) (m/cata [?v ?bindings]) & (m/cata [?rest ?bindings])}
[?x {?x ?v :as ?bindings}]
(m/cata [?v ?bindings])
?x
?x))
;; =>
["here is some data for" "Mark" {:state "California", :name "Mark"}]
👍 3