meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
2020-12-08T21:04:44.443700Z

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 2020-12-09T15:06:07.445900Z

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.

jimmy 2020-12-08T21:53:45.444Z

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.

2020-12-08T21:56:54.444300Z

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

jimmy 2020-12-08T22:06:35.444500Z

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

2020-12-08T22:08:46.444700Z

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

jimmy 2020-12-08T22:21:45.445Z

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

2020-12-08T22:32:29.445200Z

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

noprompt 2020-12-08T23:26:07.445400Z

@mac It sounds like you want interpretation which the PR @jimmy 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.

2020-12-08T23:27:44.445700Z

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