My first laborious attempt involved extracting subseqs of untouched items, mapping the function on the remaining items then joining everything back. Bleh! (Really, I don't like indices but this time I haven't found a way to work around.)
I was sad when, suddenly, I found this shortest solution:
(defn mapsplice [f coll indices]
(let [need-splice (map (set indices) (iterate inc 0))]
(mapcat #(if %2 (f %1) [%1]) coll need-splice)))
;; (mapsplice #(repeat % %) (range 10) [7 3])
;; (0 1 2 3 3 3 4 5 6 7 7 7 7 7 7 7 8 9)
No comments:
Post a Comment