Fork me on GitHub
#specter
<
2016-12-27
>
stephenmhopper18:12:03

I'm brand new to Specter, but I have a very simple task that I need to perform that I believe will be easy with Specter. I'm just not sure how to do it. I have a Clojure map and a function. I want to apply that function to the value at a given key any time that key appears in the map or any of the child maps. Does that make sense? How would I do that?

nathanmarz18:12:56

@stephenmhopper it's a recursive map?

nathanmarz19:12:29

@stephenmhopper here's an example

(let [all-maps (recursive-path [] p (continue-then-stay MAP-VALS map? p))]
  (transform [all-maps (must "mykey")] inc {"mykey" 1 :b {:a 2 :c {"mykey" 2}}})
  )

stephenmhopper19:12:07

Cool. That works. Thank you! Now I'm going to go read the docs (again) and I'll be back with questions

stephenmhopper19:12:36

Okay, I think I understand this. (recursive-path [] p (continue-then-stay MAP-VALS map? p)) generates a navigator which steps through the parameter data structure, keeping only the values which are maps. It does this recursively and because order isn't really important here, we could use stay-then-continue interchangeably with continue-then-stay. This navigator is then combined with (must "mykey") to (1) filter out maps which don't contain "mykey" and (2) expose the values associated with "mykey". inc is then mapped across these values and the rest is easy. Is that accurate?

stephenmhopper20:12:02

@nathanmarz Stage 2: How do I update the previous example to also handle maps which are nested inside of sequences?

stephenmhopper21:12:06

nvmd on Stage 2. I figured it out

nathanmarz21:12:07

@stephenmhopper yes, in this case continue-then-stay can be used interchangeably with stay-then-continue