meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
timothypratley 2020-01-19T04:35:16.020500Z

quoting…

timothypratley 2020-01-19T04:36:20.020600Z

(ns algopop.leaderboardx.ascratch
  (:require [meander.substitute.syntax.epsilon :as ss]
            [meander.substitute.epsilon :as s]
            [meander.epsilon :as m]))

(defn substitute [env pattern]
  (-> (ss/parse pattern env)
      (s/compile env)))

(defn entity-editor []
  (substitute
   '{titles ["A" "B" "C"]
     values [1 2 3]}
   '[:div.form-inline
     [:h3 heading]
     [:table.table.table-responsive.panel.panel-default
      [:thead . [:tr . [:td titles] ...]]
      [:tbody . [:tr . [:td values] ...] ...
       [:tr . [:td] ...]]]]))

(entity-editor)
^^ is there a way to do this? (this code doesn’t work)

jimmy 2020-01-19T04:40:04.020900Z

Not 100% sure what you are wanting to accomplish.

timothypratley 2020-01-19T04:40:07.021100Z

To be honest I’m not even sure what I’m asking here. I think I might be asking for a runtime feature (which I believe is planned but not something available yet)? But I’m also not convinced I need a runtime feature.

jimmy 2020-01-19T04:41:42.021300Z

(defn do-some-stuff [!things !stuff]
  (m/subst {:things [!things ...]
            :stuff [!stuff ...]}))

(do-some-stuff [1 2 3] ["a" "b" "c"])

jimmy 2020-01-19T04:42:14.021500Z

Is there something you are wanting to do that something like that can’t accomplish?

timothypratley 2020-01-19T04:46:43.021700Z

I’m mainly wondering is whether I can compile a quoted pattern

timothypratley 2020-01-19T04:48:16.021900Z

The circumstances this seems appealing are: a) Editors like cursive treat quoted forms differently (will complain about undefined symbols when not quoted) b) Loading a pattern definition from a file.

jimmy 2020-01-19T04:52:26.022100Z

So you want to quote the whole expression but also have it substitute?

timothypratley 2020-01-19T04:59:19.022300Z

The specific context I’m thinking of is that I want to allow users to customize how a html element is rendered by providing a hiccup snippet like [:h1 !title] ;; so I was looking in the substitute macro and it looks like it just calls parse and compile, so it seemed like I should be able to do that too haaaahhahaha but I don’t really grok why it doesn’t work.

jimmy 2020-01-19T05:06:31.022500Z

I mean, fixing up your entity-editor pattern makes it do something. But not something I think you want.

(defn entity-editor []
  (substitute
   '{!titles ["A" "B" "C"]
     !values [1 2 3]}
   '[:div.form-inline
     [:h3 heading]
     [:table.table.table-responsive.panel.panel-default
      [:thead . [:tr . [:td !titles] ...]]
      [:tbody . [:tr . [:td !values] ...] ...
       [:tr . [:td]]]]]))
I think you are looking for runtime features that we just don’t provide.

timothypratley 2020-01-19T05:40:38.022700Z

oh right my pattern was bogus 🤦 thank you… I can make this work like so:

(ns algopop.leaderboardx.ascratch
  (:require [meander.substitute.syntax.epsilon :as ss]
            [meander.substitute.epsilon :as s]
            [meander.epsilon :as m]))

(def ^:dynamic !titles [])
(def ^:dynamic !values [])

(defn substitution [pattern]
  (-> (ss/parse pattern)
      (s/compile {})))

(def k
  (substitution
   '[:div.form-inline
     [:table.table.table-responsive.panel.panel-default
      [:thead . [:tr . [:td !titles] ...]]
      [:tbody . [:tr . [:td !values] ...] ...]]]))

(binding [!titles ["A" "B" "C"]
          !values [1 2 3]]
  (eval k))
=>
[:div.form-inline
 [:table.table.table-responsive.panel.panel-default
  [:thead [:tr [:td "A"] [:td "B"] [:td "C"]]]
  [:tbody [:tr [:td 1] [:td 2] [:td 3]]]]]

timothypratley 2020-01-19T05:49:01.022900Z

and without dynamic vars:

(eval `(defn ~'ff [~'!titles ~'!values] ~k))

timothypratley 2020-01-19T05:49:02.023100Z

🙂