Nested lamdas: MapThreading a MapAt lambda
MapThread[MapAt[z \[Function] z/#2, #1, {All, 2}] &, {r`data, r`divs}]
or
MapThread[MapAt[Function[z, z/#2], #1, {All, 2}] &, {r`data, r`divs}]
or
MapThread[Function[{x, y}, MapAt[#/y &, x, {All, 2}]], {r`data, r`divs}]
{{{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {2, 2}, {3, 3}, {4, 4}}}
Alternatively,
Transpose[Transpose[#]/{1, #2}] & @@@ Transpose[{r`data, r`divs}]
MapThread[Transpose[Transpose[#]/{1, #2}] &, {r`data, r`divs}]
MapThread[ReplacePart[#, {i_, 2} :> #[[i, 2]]/#2] &, {r`data, r`divs}]
SubsetMap[Flatten[Partition[#, First[Length /@ r`data]]/r`divs] &, r`data,
{All, All, 2}]
Module[{z = r`data}, z[[All, All, 2]] = z[[All, All, 2]]/r`divs;z]
all give
{{{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {2, 2}, {3, 3}, {4, 4}}}
And... a Halloween special:
☺[{r`data, r`divs}]
{{{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {2, 2}, {3, 3}, {4, 4}}}
By using ReplacePart
in place of any mapping function, there is no need to define any function, neither explicitly like your r`f
nor as a pure function.
data = Thread[{Range @ 4, # Range @ 4}]& /@ (10 Range @ 3);
divisors = 10 Range @ 3;
ReplacePart[
data,
{i_, j_} :> Module[{m = data[[i, j]]}, m[[2]] = m[[2]]/divisors[[i]]; m]]
{{{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {2, 2}, {3, 3}, {4, 4}}}
In Mathematica version 12.1+ we can use OperatorApplied to inject the outer argument into the inner function:
MapThread[MapAt[OperatorApplied[#/#2&][#2], #1, {All, 2}] &, {r`data, r`divs}]
(*
{{1, 1}, {2, 2}, {3, 3}, {4, 4}},
{{1, 1}, {2, 2}, {3, 3}, {4, 4}},
{{1, 1}, {2, 2}, {3, 3}, {4, 4}}
*)
Curry from version 11.3+ does the same thing but is now considered obsolete. For more details on these and related constructions, see (197168).