Fork me on GitHub
#specter
<
2016-07-30
>
wei02:07:38

I came up with this:

(defn walk-with-index [m f]
  (let [counter (atom -1)]
    (com.rpl.specter.macros/transform [(com.rpl.specter/walker map?)]
                                      #(f (swap! counter inc) %) m)))

wei02:07:11

there’s definitely a better way to do it, looking forward to any feedback 🙂

codonnell03:07:11

@wei: here's a stateless solution:

(transform (subselect (walker map?)) (partial map #(assoc %2 :id %1) (range)) [[{} {}] {} {}])
[[{:id 0} {:id 1}] {:id 2} {:id 3}]

codonnell03:07:34

no idea how that compares to your atom solution in terms of efficiency, though

wei03:07:53

@codonnell: looks better though, thanks!

nathanmarz06:07:29

@wei @codonnell: this solution is even more concise:

(setval (subselect (walker map?) :id)
  (range)
  [[{} {}] [{} {} [{}]]])
;; => [[{:id 0} {:id 1}] [{:id 2} {:id 3} [{:id 4}]]]

codonnell14:07:16

@nathanmarz: Just checked out multi-transform, terminal, and terminal-val and added them to the wiki.