MapAt
[f, expr, n]MapAt
[f, expr, {i, j ...}]MapAt
[f, pos]MapAt
that can be applied to an expression.Map function f to the second element of an simple flat list:
MapAt[f, {a, b, c}, 2]
Above, we specified a simple integer value 2. In general, the expression can be an arbitrary vector.
Using MapAt
with Function[0]
, we can zero a value or values in a vector:
MapAt[0&, {{1, 1}, {1, 1}}, {2, 1}]
When the dimension of the replacement expression is less than the vector, that element's dimension changes:
MapAt[0&, {{0, 1}, {1, 0}}, 2]
So now compare what happen when using {{2}, {1}} instead of {2, 1} above:
MapAt[0&, {{0, 1}, {1, 0}}, {{2}, {1}}]
Map f onto the last element of a list:
MapAt[f, {a, b, c}, -1]
Same as above, but use the operator form of MapAt
:
MapAt[f, -1][{a, b, c}]
Map f onto at the second position of an association:
MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, 2]
Same as above, but select the second-from-the-end position:
MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, -2]