Skip to content

MapAt ​

f applied to the part of expr at position n, leaving the rest of expr unchanged.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageMapAt✓
MapAt(f, expr, n)f applied at the (1-based, negative counts from the end) position n
MapAt(f, expr, {{n1}, {n2}, …})f applied independently at each of several top-level positions
Typed signatures
TypePackageApplies
((any) -> any, any, integer | list<list<integer>>) -> anycombinatoricsalways

Domain: Collections

Details
  • A position named twice gets f applied twice there.
  • Only top-level positions are answered here -- a nested path (into a sub-list) is left unevaluated.

Examples

Basic #
live ↗
InMapAt(f,[a,b,c],2)\mathrm{MapAt}(f, \bigl\lbrack a, b, c\bigr\rbrack, 2)Out[a,f(b),c]\bigl\lbrack a, f(b), c\bigr\rbrack

Applies ff at one position only

live ↗
InMapAt(f,[a,b,c,d],2)\mathrm{MapAt}(f, \bigl\lbrack a, b, c, d\bigr\rbrack, 2)Out[a,f(b),c,d]\bigl\lbrack a, f(b), c, d\bigr\rbrack

A list of positions applies ff at several places

live ↗
InMapAt(f,[a,b,c,d],[[1],[4]])\mathrm{MapAt}(f, \bigl\lbrack a, b, c, d\bigr\rbrack, \bigl\lbrack\bigl\lbrack1\bigr\rbrack, \bigl\lbrack4\bigr\rbrack\bigr\rbrack)Out[f(a),b,c,f(d)]\bigl\lbrack f(a), b, c, f(d)\bigr\rbrack
Scope #

A negative position counts from the end

live ↗
InMapAt(f,[a,b,c],−1)\mathrm{MapAt}(f, \bigl\lbrack a, b, c\bigr\rbrack, -1)Out[a,b,f(c)]\bigl\lbrack a, b, f(c)\bigr\rbrack

A list of positions applies f at each independently

live ↗
InMapAt(f,[a,b,c],[[1],[3]])\mathrm{MapAt}(f, \bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack\bigl\lbrack1\bigr\rbrack, \bigl\lbrack3\bigr\rbrack\bigr\rbrack)Out[f(a),b,f(c)]\bigl\lbrack f(a), b, f(c)\bigr\rbrack

Works inside a sum, where the result is reordered into canonical form

live ↗
InMapAt(f,a+b+c+d,2)\mathrm{MapAt}(f, a+b+c+d, 2)Outa+c+d+f(b)a+c+d+f(b)

Position 22 is a whole sublist, which hh wraps

live ↗
InMapAt(h,[[a,b,c],[d,k],f,g],2)\mathrm{MapAt}(h, \bigl\lbrack\bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack d, k\bigr\rbrack, f, g\bigr\rbrack, 2)Out[[a,b,c],h([d,k]),f,g]\bigl\lbrack\bigl\lbrack a, b, c\bigr\rbrack, h(\bigl\lbrack d, k\bigr\rbrack), f, g\bigr\rbrack

A negative position counts from the end, so −3-3 is the second of four

live ↗
InMapAt(h,[[a,b,c],[d,k],f,g],−3)\mathrm{MapAt}(h, \bigl\lbrack\bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack d, k\bigr\rbrack, f, g\bigr\rbrack, -3)Out[[a,b,c],h([d,k]),f,g]\bigl\lbrack\bigl\lbrack a, b, c\bigr\rbrack, h(\bigl\lbrack d, k\bigr\rbrack), f, g\bigr\rbrack

Several top-level positions applied together

live ↗
InMapAt(h,[[a,b,c],[d,k],f,g],[[2],[1]])\mathrm{MapAt}(h, \bigl\lbrack\bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack d, k\bigr\rbrack, f, g\bigr\rbrack, \bigl\lbrack\bigl\lbrack2\bigr\rbrack, \bigl\lbrack1\bigr\rbrack\bigr\rbrack)Out[h([a,b,c]),h([d,k]),f,g]\bigl\lbrack h(\bigl\lbrack a, b, c\bigr\rbrack), h(\bigl\lbrack d, k\bigr\rbrack), f, g\bigr\rbrack
Properties #
live ↗
InMapAt(f,[a,b,c,d],[[1],[3],[1]])\mathrm{MapAt}(f, \bigl\lbrack a, b, c, d\bigr\rbrack, \bigl\lbrack\bigl\lbrack1\bigr\rbrack, \bigl\lbrack3\bigr\rbrack, \bigl\lbrack1\bigr\rbrack\bigr\rbrack)Out[f(f(a)),b,f(c),d]\bigl\lbrack f(f(a)), b, f(c), d\bigr\rbrack
About
Examples10
Checked againstWolfram Language 10 agree
CrosswalkWolfram Language