There's clojure.core/map-indexed
; it's similar but not exactly what you're looking for.
E.g.
(map-indexed vector '[a b c]) ;=> '([0 a] [1 b] [2 c])
This is awfully close to what I think you're going for:
(defn map-depth
"Maps each element of s with an array of indices, as used by e.g. update-in,
optionally at the given depth"
([f s depth path]
(if (or (and depth (zero? depth))
(not (sequential? s)))
(f s path)
(map-indexed (fn [i e]
(map-depth f e
(when depth (dec depth))
(conj path i)))
s)))
([f s depth]
(map-depth f s depth []))
([f s]
(map-depth f s nil [])))
E.g.
(map-depth vector '[a [b c] d]) ;=> '([a [0]] ([b [1 0]] [c [1 1]]) [d [2]])
(map-depth vector '[a b c] 0) ;=> '[[a b c] []]
(map-depth vector '[[a b] [a b] [a b]] 1) ;=> '([[a b] [0]] [[a b] [1]] [[a b] [2]])
Are you coming from a Mathematica background, though?
It's important to remember also that Clojure's +
operator doesn't play nicely with lists.
In Mathematica you can do
{1, 2, 3, 4} + 2 (* -> {3 4 5 6} *)
But Clojure will complain; you have to work around it.
(+ [1 2 3 4] 2) ;=> ClassCastException
(map (partial + 2) [1 2 3 4]) ;=> (3 4 5 6)