specter

Latest version: 1.1.3
2018-05-08T15:45:21.000155Z

I have a vector of maps:

[{:a 1} {:b 2 :d 4} {:c 3} {:d 4}]
and I want to find the map where :c = 3 and insert another map {:x 100} before it, resulting in
[{:a 1} {:b 2 :d 4} {:x 100} {:c 3} {:d 4}]
What's the best way to accomplish this in specter?

2018-05-08T15:46:43.000239Z

(I don't know the index I need to insert at, just the keys in the map I need to insert before )

2018-05-08T15:49:19.000765Z

I've been messing with INDEXED-VALS and trying to collect the index and then insert the new map, but I'm having trouble getting this to work and am not sure this is the right line of thinking or if there's a better way

nathanmarz 2018-05-08T16:05:33.000325Z

@jjttjj I would do that with a select followed by a transform:

(if-let [i (select-first [INDEXED-VALS (selected? LAST :c (pred= 3)) FIRST)] data)]
  (setval (before-index i) {:x 100} data))

1
nathanmarz 2018-05-08T16:06:01.000077Z

you can also do it in a single operation using zippers

2018-05-08T16:06:44.000088Z

cool, that one works, thanks!