Fork me on GitHub
#meander
<
2020-10-10
>
nivekuil07:10:25

ok, here's another one that I think is doable, but can't figure out: find an elt in vec that is a map with the key :a, and wrap it with a list with something arbitrary added like: [1 2 {:a 1} {:d 4}] => [1 2 ({:a 1} 99) {:d 4}]

Lucy Wang07:10:12

use the subtree search operator m/$

(m/match [1 2 {:a 1} {:d 4}]
  (m/$ ?context {:a ?a})
  (?context (list {:a ?a} 99)))
;; => [1 2 ({:a 1} 99) {:d 4}]

nivekuil07:10:44

aha, so that's what that does. I was confused as to how you get the whole map, but looks like that's what ?context is for. thanks

nivekuil08:10:49

I saw it, but my brain is too slow at this time to understand it :) I am also too slow to understand why it only seems to match the first map, so it wouldn't work if I wanted to match on`:d`:

(m/match [1 2 {:a 1} {:d 4}]   
 (m/$ ?context {:d ?d})
 (?context (list {:d ?d} 99)))  
 => [1 2 ({:d nil} 99) {:d 4}]