Fork me on GitHub
#meander
<
2021-04-19
>
Richie02:04:06

Hey, I’m trying to get started with meander.

(let [data {:a {:b "hi"}}]
  (let [?x (m/match data
                    {:a {:b ?x}}
                    ?x)]
   (m/subst {:a {:b ?x}})))
This works fine but I don’t want to repeat the shape that I’m matching against.
(let [data {:a {:b "hi"}}]
  (let [shape '{:a {:b ?x}}
        ?x (m/match data
                    ~shape
                    ?x)]
    (m/subst ~shape)))
How do I pull the shape out?

Richie02:04:28

More context for what I’m trying to accomplish: I want to get data out, apply a function, and then put the data back. I started with lenses but that got messy fast.

(lens/over
 (lens/in [:a :b])
 reverse
 {:a {:b "hi"}}) ;; {:a {:b ("i" "h")}}

Jimmy Miller02:04:00

So for meander, if you find yourself quoting things to try and make things more concise, you are probably not going to have a great time. We do have an interpreter for more dynamic things. But, in general, we value clarity over concision. So, you might repeat yourself a couple times, but we think it pays off. Here are two examples that might help with the sorts of things you are looking to do.

(m/match {:a {:b "hi"}}
  {:a {:b ?x}}
  {:a {:b (string/reverse ?x)}})


(m/rewrite {:a {:b ["things" "here"]}}
  {:a {:b [!xs ...]}}
  {:a {:b [(m/app string/reverse !xs) ...]}})

Richie12:04:05

Ok, thank you.