Fork me on GitHub
#meander
<
2021-04-27
>
Ben Sless18:04:49

Hello, I'm playing around with meader trying to replicate destructuring, and I can't figure out the correct accumulator syntax This naive rewrite works:

(let [s
      (m*/rewrite
       [[!b '& ?bs] [!x & ?xs]]
       [!b !x
        ?bs ?xs])]

  (s '[[x & xs] [1 2 3]]))
But anything I've tried with accumulators turned out wrong. Any ideas?

Jimmy Miller20:04:34

A couple things. Not 100% sure what you mean by accumulators. I'm also guessing you are using strategies. Any reason? Finally an example of what isn't working would be super helpful for knowing how we can help. Not sure if someone has reimplemented drstructuring already using meander, but should definitely be possible.

Ben Sless04:04:23

Thank you Here's an example where the final pattern fails

(let [s
      (m*/rewrite
       [[!b ..?n '& ?bs] [!x ..?n & ?xs]]
       [!b !x ..?n
        ?bs ?xs])]

  [(s '[[x & xs] [1 2 3]])
   (s '[[x y & xs] [1 2 3]])
   (s '[[x y z & xs] [1 2 3]])
   (s '[[x y z & xs] [1 2]])])

Jimmy Miller14:04:02

Yeah, that fails because you are saying that the repeats for both vectors should be the same. That is what using ?n twice does. So on that last example, there are 3 elements before the & and there are only two on the right hand side. So they don’t match.

Ben Sless14:04:22

Well, it's pretty clear they don't match, I just provided some examples of stuff I tired. I'm not sure what would work correctly, though :thinking_face:

Jimmy Miller14:04:18

Well, it depends on what you want. I’m guessing you want to assign z to nil and xs to nil in that least example? If so, you can do something like this

(let [s
      (m*/rewrite
       [[!b ..?n '& ?bs] [!x ..?n & ?xs]]
       [!b !x ..?n
        ?bs ?xs]

       [[!b ..?n '& ?bs] [!x ...]]
       [!b !x ..?n
        ?bs nil])]
  [(s '[[x & xs] [1 2 3]])
   (s '[[x y & xs] [1 2 3]])
   (s '[[x y z & xs] [1 2 3]])
   (s '[[x y z & xs] [1 2]])])

Ben Sless14:04:59

Ah, the second pattern is for when the first doesn't match then it all gets accumulated to !x?

👍 2
Jimmy Miller15:04:43

Yep exactly right.