To understand grouping and precedence, use HoldForm
and PrecedenceForm
. I'll insert a screenshot to make the output clearer:
It is useful to know that //
has even lower precedence than &
and can save you some parentheses.
You probably meant:
({#[[1]], #[[3]]} &) /@ ({#[[1]], #[[2]], #[[3]]} &) /@ {{"A", "B", "C"}, {1, 2, 3}}
It is useful to parenthesize the entire function because &
has very low precedence, lower than most other operators. Thus it tends to act on everything preceding it.
Another common mistake with &
is using it like this in options:
SomeFunction -> #&
This is really (SomeFunction -> #)&
and not SomeFunction -> (#&)
.
One of the few operators that have even lower precedence than &
is //
. Thus this is safe:
argument // #&
It groups as argument // (#&)
and not as (argument // #)&
.
Alternative ways to write you expression are:
Map[{#[[1]], #[[3]]} &] @ Map[{#[[1]], #[[2]], #[[3]]} &] @ {{"A", "B", "C"}, {1, 2, 3}}
{{"A", "B", "C"}, {1, 2, 3}} // Map[{#[[1]], #[[2]], #[[3]]} &] // Map[{#[[1]], #[[3]]} &]
You may or may not find these more readable than the explicitly parenthesized version.
Recently I prefer the latter when doing a lot of chaining.
{{"A", "B", "C"}, {1, 2, 3}}[[All, {1, 3}]]
? You might also be interested in Elegant operations on matrix rows and columns. $\endgroup$"Part specification \!\(\"A\"[[1]]\) is longer than depth of object."
, and returns something like generic result{{{"A"[[1]], "A"[[3]]}, {"B"[[1]], "B"[[3]]}, {"C"[[1]], "C"[[3]]}}, {{1[[1]], 1[[3]]}, {2[[1]], 2[[3]]}, {3[[1]], 3[[3]]}}}
Expected result should be as in 2nd case. $\endgroup$Map
function works on it's arguments. $\endgroup$