# MapIndexed stops at levelspec 1 if only the index is passed?

If the function passed to MapIndexed references the first (or data) argument, the following performs as expected:

MapIndexed[#2 &, 1 -> {2 -> {3 -> 4}}, Infinity]


As well as

MapIndexed[(#1; #2) &, 1 -> {2 -> {3 -> 4}}, Infinity]


yield:

{1} -> {2}


Not respecting the Infinity level spec,

whereas:

MapIndexed[{#1, #2} &, 1 -> {2 -> {3 -> 4}}, Infinity]


gives

{1, {1}} -> {{{{2, {2, 1,
1}} -> {{{{3, {2, 1, 2, 1, 1}} -> {4, {2, 1, 2, 1, 2}}, {2, 1,
2, 1}}}, {2, 1, 2}}, {2, 1}}}, {2}}

-
You can show it on simple example: MapIndexed[#2 &, 1 -> {2 -> {3 -> 4}}, Infinity]. #2& is loosing information about # so this is what I'd expect, isn't it? – Kuba Apr 24 '14 at 21:16
beat me too it.. suggest you edit the question with a simpler example. The example has some syntax error anyway. – george2079 Apr 24 '14 at 21:18
@Kuba I think it's not so simple. We could imagine a different action, where all parts are simply replaced by their positions, while heads are preserved. I think, they key point is that MapIndexed acts depth-first, leaves before branches. No idea why this one was down voted. – Leonid Shifrin Apr 24 '14 at 21:26
@Kuba May be you're right. If we anyway replace the element with position, we probably lose this info either way. – Leonid Shifrin Apr 24 '14 at 21:30
I didn't downvote, but I do think someone with 2k rep should know to provide a minimal example.. You can see the same with lists by the way eg MapIndexed[#2 &, {a, {b, c}}, Infinity] – george2079 Apr 24 '14 at 21:31

This already seems to be answered in the comments, but since some 40+ minutes have passed I'll post it myself.

Using george's example and Trace it's pretty clear what happens:

MapIndexed[#2 &, {a, {b, c}}, Infinity]


Is transformed into:

{(#2&)[a,{1}],(#2&)[{(#2&)[b,{2,1}],(#2&)[c,{2,2}]},{2}]}


From there it evaluates as usual and so you end up with parts like:

(#2&)[{{2,1},{2,2}},{2}]


Where the inner data that you constructed is thrown away, leaving only {2}, producing a final expression of:

{{1}, {2}}

-
@Kuba I only saw this question a few minutes ago, but since you had the answer long before and didn't post it I assumed you were not going to. – Mr.Wizard Apr 24 '14 at 21:48
@Mr.Wizard, ok thanks. How do you suggest mapping all the index positions in the expression? Seems a hack to pass the #1 parameter and later project it out. – alancalvitti Apr 24 '14 at 23:29
@alancalvitti It is not clear to me what output you expect. Would you please post a new question? – Mr.Wizard Apr 25 '14 at 1:10