That is to say, a generalized map
.
E.g., here is a nested list:
nl := [[[[s, t]], [u, [v, w]]], [[x, [y, z]]]]:
We can use map
to apply the mapped function F to "each operand" (i.e., the first‐level parts) of :
:-map(F, nl);
=
[F([[[s, t]], [u, [v, w]]]), F([[x, [y, z]]])]
But in Mathematica, we can make further explorations:
In[1]:= nl = {{{{s, t}}, {u, {v, w}}}, {{x, {y, z}}}};
In[2]:= Map[F, nl, {1}] (*Maple's result*)
Out[2]= {F[{{{s, t}}, {u, {v, w}}}], F[{{x, {y, z}}}]}
In[3]:= Map[F, nl, {2, -2}]
Out[3]= {{F[{F[{s, t}]}], F[{u, F[{v, w}]}]}, {F[{x, F[{y, z}]}]}}
In[4]:= Map[F, nl, {-3, 3}]
Out[4]= {{F[{F[{s, t}]}], F[{F[u], F[{v, w}]}]}, {F[{F[x], F[{y, z}]}]}}
In[5]:= Map[F, nl, {0, \[Infinity]}, Heads -> \[Not] True]
Out[5]= F[{F[{F[{F[{F[s], F[t]}]}], F[{F[u], F[{F[v], F[w]}]}]}], F[{F[{F[x], F[{F[y], F[z]}]}]}]}]
Note that the last case has been implemented in Maple as MmaTranslator[Mma][MapAll]
:
MmaTranslator:-Mma:-MapAll(F,nl);
=
F([F([F([F([F(s), F(t)])]), F([F(u), F([F(v), F(w)])])]),
F([F([F(x), F([F(y), F(z)])])])])
Naturally, how to reproduce the other two results in Maple programmatically? (The output may not be easy to read or understand; I have added an addendum below.)
Addendum. It is also possible to display in "tree" structure (like dismantle
) manually:
`[]`
(
`[]`
(
`[]`
(
`[]`
(
s
,
t
)
)
,
`[]`
(
u
,
`[]`
(
v
,
w
)
)
)
,
`[]`
(
`[]`
(
x
,
`[]`
(
y
,
z
)
)
)
)
As you can see, the "depth" of is five (0, 1, 2, 3, and 4), while the classical map
just maps at the first "level". (Moreover, such descriptions may lead to a confusion.)
Supplement. Unfortunately, there remains a bug in the MmaTranslator[Mma][Level]
. Compare:
MmaTranslator:-Mma:-Level(nl, [4]); (*Maple*)
[v, w]
MmaTranslator:-Mma:-Level(nl, [-1]); (*Maple*)
[s, t, u, v, w, x, y, z, -1, x, c, r, y, 2]
In[6]:= Level[nl, {4}] (*Mathematica*)
Out[6]= {s, t, v, w, y, z}
In[7]:= Level[nl, {-1}] (*Mathematica*)
Out[7]= {s, t, u, v, w, x, y, z}