Fork me on GitHub
#meander
<
2020-12-08
>
mac21:12:44

I am trying to pass patterns into a function that applies these using search but I am doing something wrong. Example which works when I hardcode the patterns into the function. I have troed unquoting the pattern inside apply-rule, but that makes no difference:

(def global-counter (java.util.concurrent.atomic.AtomicInteger. 0))
(defn next-id [] (.getAndIncrement global-counter))

(defn apply-rule [graph rule]
  (let [[pattern out] rule]
      (map vec
       (partition
        2
        (flatten
         (me/search
           graph
           (me/scan pattern)
           out))))))

(let [g [[(next-id) (next-id)]]]
  (apply-rule g '[[?x ?y] [[(next-id) ?y] [?y ?x]]]))

Jimmy Miller21:12:45

Right now this isn’t possible because meander works by compiling patterns. This makes these dynamic things not work. We are actively working on an interpreter though https://github.com/noprompt/meander/pull/155 If you are doing this for some other reason than wanting dynamic patterns, we can definitely help with that too if you explain a bit more about your motivation. In the example you gave it is hard for me to tell if you really need things to be dynamic.

mac21:12:54

I was looking to pass the patterns to have a concise way to let the user play around with different patterns.

Jimmy Miller22:12:35

Yeah right now the only way do things dynamically like that would be to eval stuff.

mac22:12:46

How would I use eval to achieve this? I am confused.

Jimmy Miller22:12:45

On my phone sadly. Can post later tonight. But if you are accepting user input, eval is not safe.

mac22:12:29

By user input, I meant just to let the user of the library pass quoted patterns into the function. Happy to wait for input.

noprompt23:12:07

@U09UV3WP6 It sounds like you want interpretation which the PR @U5K8NTHEZ linked earlier will provide support once it is merged. I have been actively working on it and expect to have it merged soon. I have shared a couple of examples demonstrating it https://clojurians.slack.com/archives/CFFTD7R6Z/p1606244106396700 and https://clojurians.slack.com/archives/CFFTD7R6Z/p1606936033435400. I’m currently trying to round a few more rough edges and going as quickly as I can. I know this is a feature folks want (including myself) and it’s my number one priority right now.

mac23:12:44

OK, that's cool. I will use hard coded patterns for now.

Jimmy Miller15:12:07

Sorry for taking so long to reply. I definitely don’t recommend doing this. But just wanted to reply with the eval version.

(def global-counter (java.util.concurrent.atomic.AtomicInteger. 0))
(defn next-id [] (.getAndIncrement global-counter))
(defn apply-rule [graph rule]
  (let [[pattern out] rule]
    (map vec
         (partition
          2
          (flatten
           (eval
            `(m/search
               ~graph
               (m/scan ~pattern)
               ~out)))))))
This will be rather slow as you would be compiling the expression every single time.