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]]]))
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.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.
I was looking to pass the patterns to have a concise way to let the user play around with different patterns.
Yeah right now the only way do things dynamically like that would be to eval stuff.
How would I use eval to achieve this? I am confused.
On my phone sadly. Can post later tonight. But if you are accepting user input, eval is not safe.
By user input, I meant just to let the user of the library pass quoted patterns into the function. Happy to wait for input.
@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.
OK, that's cool. I will use hard coded patterns for now.