Apply multiple functions to parts of a nested list
data = {{1, a, x, "one"}, {2, b, y, "two"}, {3, c, z, "three"}};
functions = {f, g, h, m};
Inner[#2[#1] &, data, functions, List]
(* Out:
{{f[1], g[a], h[x], m["one"]},
{f[2], g[b], h[y], m["two"]},
{f[3], g[c], h[z], m["three"]}}
*)
With reversing the order of functions
and data
, to fully harness the functional style without using slots:
Inner[Compose, functions, Transpose@data, List]
data // Replace[#, {a_, b_, c_, d_} :> {f@a, g@b, h@c, m@d}, 1] &
Not quite as nice and concise as the Inner
solution, but still worth writing down I think:
MapThread[#1@#2 &, {Table[functions, {Length@data}], data}, 2]
MapThread[Compose, {Table[functions, {Length@data}], data}, 2]
or
MapThread[#1@#2 &, {functions, #}] & /@ data
MapThread[Compose, {functions, #}] & /@ data
or