This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-12-08
Channels
- # adventofcode (55)
- # announcements (21)
- # babashka (20)
- # beginners (89)
- # calva (21)
- # cider (16)
- # circleci (11)
- # clara (8)
- # clj-kondo (6)
- # clojure (31)
- # clojure-australia (3)
- # clojure-europe (17)
- # clojure-nl (5)
- # clojure-uk (10)
- # clojurescript (25)
- # community-development (4)
- # conjure (12)
- # cryogen (28)
- # cursive (21)
- # datomic (7)
- # deps-new (1)
- # depstar (45)
- # emacs (5)
- # fulcro (46)
- # instaparse (5)
- # jobs (5)
- # jobs-discuss (23)
- # kaocha (12)
- # lambdaisland (2)
- # leiningen (1)
- # meander (10)
- # mid-cities-meetup (1)
- # reagent (5)
- # reitit (5)
- # remote-jobs (45)
- # reveal (9)
- # sql (6)
- # tools-deps (103)
- # uncomplicate (1)
- # xtdb (1)
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]]]))
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.
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.
@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.
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.