Fork me on GitHub
#specter
<
2018-05-08
>
jjttjj15:05:21

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?

jjttjj15:05:43

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

jjttjj15:05:19

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

nathanmarz16:05:33

@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))

4
nathanmarz16:05:01

you can also do it in a single operation using zippers

jjttjj16:05:44

cool, that one works, thanks!