Applying Functions to Parts of Expressions

If you have a list of elements, it is often important to be able to apply a function separately to each of the elements. You can do this in the Wolfram Language using (RefLink[Map, paclet:ref/Map]).

This applies f separately to each element in a list:

Map[f,{a,b,c}]

This defines a function which takes the first two elements from a list:

take2[list_]:=Take[list,2]

You can use (RefLink[Map, paclet:ref/Map]) to apply take2 to each element of a list:

Map[take2,{{1,3,4},{5,6,7},{2,1,6,6}}]

Applying a function to each element in a list.

What (RefLink[Map, paclet:ref/Map])[f,expr] effectively does is to "wrap" the function f around each element of the expression expr. You can use (RefLink[Map, paclet:ref/Map]) on any expression, not just a list.

This applies f to each element in the sum:

Map[f,a+b+c]

This applies (RefLink[Sqrt, paclet:ref/Sqrt]) to each argument of g:

Map[Sqrt,g[x^2,x^3]]

(RefLink[Map, paclet:ref/Map])[f,expr] applies f to the first level of parts in expr. You can use (RefLink[MapAll, paclet:ref/MapAll])[f,expr] to apply f to all the parts of expr.

This defines a 2x2 matrix m:

m={{a,b},{c,d}}

(RefLink[Map, paclet:ref/Map]) applies f to the first level of m, in this case the rows of the matrix:

Map[f,m]

(RefLink[MapAll, paclet:ref/MapAll]) applies f at all levels in m. If you look carefully at this expression, you will see an f wrapped around every part:

MapAll[f,m]

In general, you can use level specifications as described in "Levels in Expressions" to tell (RefLink[Map, paclet:ref/Map]) to which parts of an expression to apply your function.

This applies f only to the parts of m at level 2:

Map[f,m,{2}]

Setting the option (RefLink[Heads, paclet:ref/Heads])->(RefLink[True, paclet:ref/True]) wraps f around the head of each part, as well as its elements:

Map[f,m,Heads->True]

Ways to apply a function to different parts of expressions.

Level specifications allow you to tell (RefLink[Map, paclet:ref/Map]) to which levels of parts in an expression you want a function applied. With (RefLink[MapAt, paclet:ref/MapAt]), however, you can instead give an explicit list of parts where you want a function applied. You specify each part by giving its indices, as discussed in "Parts of Expressions".

Here is a 2x3 matrix:

mm={{a,b,c},{b,c,d}}

This applies f to parts {1,2} and {2,3}:

MapAt[f,mm,{{1,2},{2,3}}]

This gives a list of the positions at which b occurs in mm:

Position[mm,b]

You can feed the list of positions you get from (RefLink[Position, paclet:ref/Position]) directly into (RefLink[MapAt, paclet:ref/MapAt]):

MapAt[f,mm,%]

To avoid ambiguity, you must put each part specification in a list, even when it involves only one index:

MapAt[f,{a,b,c,d},{{2},{3}}]

Applying a function to specific parts of an expression.

Here is an expression:

t=1+(3+x)^2/x

This is the full form of t:

FullForm[t]

You can use (RefLink[MapAt, paclet:ref/MapAt]) on any expression. Remember that parts are numbered on the basis of the full forms of expressions:

MapAt[f,t,{{2,1,1},{2,2}}]

Applying a function to parts and their indices.

This applies f to each element in a list, giving the index of the element as a second argument to f:

MapIndexed[f,{a,b,c}]

This applies f to both levels in a matrix:

MapIndexed[f,{{a,b},{c,d}},2]

(RefLink[Map, paclet:ref/Map]) allows you to apply a function of one argument to parts of an expression. Sometimes, however, you may instead want to apply a function of several arguments to corresponding parts of several different expressions. You can do this using (RefLink[MapThread, paclet:ref/MapThread]).

Applying a function to several expressions at once.

This applies f to corresponding pairs of list elements:

MapThread[f,{{a,b,c},{ap,bp,cp}}]

(RefLink[MapThread, paclet:ref/MapThread]) works with any number of expressions, so long as they have the same structure:

MapThread[f,{{a,b},{ap,bp},{app,bpp}}]

Functions like (RefLink[Map, paclet:ref/Map]) allow you to create expressions with parts modified. Sometimes you simply want to go through an expression, and apply a particular function to some parts of it, without building a new expression. A typical case is when the function you apply has certain “side effects”, such as making assignments, or generating output.

Evaluating functions on parts of expressions.

(RefLink[Map, paclet:ref/Map]) constructs a new list in which f has been applied to each element of the list:

Map[f,{a,b,c}]

(RefLink[Scan, paclet:ref/Scan]) evaluates the result of applying a function to each element, but does not construct a new expression:

Scan[Print,{a,b,c}]

(RefLink[Scan, paclet:ref/Scan]) visits the parts of an expression in a depth-first walk, with the leaves visited first:

Scan[Print,1+x^2,Infinity]