https://www.reddit.com/r/Clojure/comments/j79imz/a_little_syntax_sugar_for_malli_using_meander/
Hey all. Very impressed my meander. I am currently using $ to find matching submaps. In that connection I would like to be able to capture the "path" to the matching maps, path in this case being not only the keys but the complete "super" maps of the match. Any recommendations on how to achieve this?
maybe with m/cata?
(m/search {:user {:name {:first "Jack" :second "Lee"}}}
{:first (m/some ?first) :as ?full}
?full
{?k (m/and ?v (m/cata ?vv))}
[?k ?v ?vv])
;; => ([:user
;; {:name {:first "Jack", :second "Lee"}}
;; [:name {:first "Jack", :second "Lee"} {:first "Jack", :second "Lee"}]])
@wxitb2017 Thanks a lot, I will give that a look.
@wxitb2017 I think I understand how this works. Some of my maps have sequences of maps as values. I tried the below to accommodate for this, but it does not appear to work.
(me/search
{:user {:name {:first "Jack" :second "Lee" :addresses [{:main "test"} {:secondary "test1"}]}}}
{:main (me/some ?main) :as ?full}
?full
(me/scan {?k (me/and ?v (me/cata ?vv))})
[?k ?v ?vv]
{?k (me/and ?v (me/cata ?vv))}
[?k ?v ?vv])
m/$
uses a zipper when context-pattern
is passed
(m/$ ?context-fn ?pattern)
I suppose we could “unshift” another arity for capturing the path when matching an, pending a sensible definition, ignore it during substitution.
(m/$ ?path ?context-fn ?pattern)
Alternatively, the ?path
parameter could be “pushed”
(m/$ ?context-fn ?pattern ?path)
or, if it makes sense, we could accept and parse keyword arguments
(m/$ ?context-fn ?pattern :path ?path :left ?left :right ?right)
Of course, we would ensure the getting the values/locations would be a safe operation.
In terms of flexibility/maintainability, keyword arguments are the most appealing because we can incrementally add all this functionality.
m/$
is meant for situations where you don’t know where something is so I think adding a way to capture more contextual information would be useful.
I think if we can come up with a sensible semantic for substitution with these additional parameters, we’d have a powerfully succinct but blunt force way of getting common tree rewrites off the ground.
What do y’all think?
@noprompt I am an absolute newb when it comes to meander, but it sounds good to me.
@mac Newbies tend to have the most affect and influence on the project. 🙂
Not to say we don’t value veterans. 😉