Map
Map[f, expr]
returns a new expression with the same head as expr
, but with f
mapped to each of the arguments.
Map[f, expr, levelspec] maps f to all subexpressions that match the level specification levelspec.
Attributes[Map] := {Protected}
Simple examples
In[1]:= Map[foo, {a, b, c}]
Out[1]= {foo[a], foo[b], foo[c]}
In[2]:= Map[foo, {a, b, c}]
Out[2]= {foo[a], foo[b], foo[c]}
In[3]:= Map[Times, {2, 4, 9}]
Out[3]= {2, 4, 9}
In[4]:= Map[foo, {{a, b}, c}]
Out[4]= {foo[{a, b}], foo[c]}
In[5]:= Map[foo]
Out[5]= Map[foo]
In[6]:= Map[foo, foo]
Out[6]= foo
In[7]:= Map[foo, foo, foo]
Out[7]= Map[foo, foo, foo]
Pure functions are useful with Map
:
In[8]:= Map[Function[x, x^2], {2, 4}]
Out[8]= {4, 16}
In[9]:= Map[Function[Slot[1]^2], {2, 4}]
Out[9]= {4, 16}
In[10]:= f[a[f[b[f[c], f[d], f[e]]], f[l[f[g[f[h], f[j]]], f[k]]]]]
Out[10]= Map[f, a[b[c, d, e], l[g[h, j], k]], {Infinity*-1, Infinity}]
In[11]:= a[f[b[f[c], f[d], f[e]]], f[l[f[g[f[h], f[j]]], f[k]]]]
Out[11]= Map[f, a[b[c, d, e], l[g[h, j], k]], Infinity]
In[12]:= a[f[b[f[c], f[d], f[e]]], l[f[g[f[h], f[j]]], f[k]]]
Out[12]= Map[f, a[b[c, d, e], l[g[h, j], k]], {-2, Infinity}]
In[13]:= a[b[c, d, e], l[f[g[h, j]], k]]
Out[13]= Map[f, a[b[c, d, e], l[g[h, j], k]], {2, -2}]
In[14]:= a[f[b[f[c], f[d], f[e]]], f[l[f[g[h, j]], f[k]]]]
Out[14]= Map[f, a[b[c, d, e], l[g[h, j], k]], 2]
In[15]:= a[f[b[c, d, e]], f[l[f[g[h, j]], k]]]
Out[15]= Map[f, a[b[c, d, e], l[g[h, j], k]], -2]