11
$\begingroup$

I am working in a Dataset and I need to create a function for a ChartStyle option of a BarChart that takes a list of the items to be plotted. I would like to lighten the plot style colour by the value of the item to be plotted. I have created a function that gets me the list of functions for each PlotStyle.

Function[{value}, Nest[Lighter, #, 3 - value]] & /@ {Yellow, Orange, Blue}

value will be an integer between 0 and 3 inclusive. Is there some use of Through or Outer or Inner or something that will map a list of length to each position in this list of functions such that:

{f1, f2, f3}[{x1, x2, x3}] -> {f1[x1], f2[x2], f3[x3]} 

This is in a Dataset query so I'm hoping for something concise that is easy to read and that is a one liner with the function list definition given above.

$\endgroup$
1

3 Answers 3

9
$\begingroup$
Inner[#1[#2] &, {f1, f2, f3}, {x1, x2, x3}, List]

(*  {f1[x1], f2[x2], f3[x3]}  *)

#[[1]][#[[2]]] & /@
 Transpose[{{f1, f2, f3}, {x1, x2, x3}}]

(*  {f1[x1], f2[x2], f3[x3]}  *)
$\endgroup$
2
  • 6
    $\begingroup$ ...and my standard solution, MapThread[#1@#2 &, {{f1, f2, f3}, {x1, x2, x3}}], which is a variant of the Inner solution. $\endgroup$
    – march
    Commented Nov 7, 2015 at 5:18
  • 1
    $\begingroup$ ... and similar to MapThread[Apply, {{f1, f2, f3}, Transpose[{{x1, x2, x3}}]}]. $\endgroup$
    – Karsten7
    Commented Nov 7, 2015 at 6:12
10
$\begingroup$
Needs["GeneralUtilities`"]
MultiMapAt[Range[3], {f1, f2, f3}][{x1, x2, x3}]

$\ ${f1[x1], f2[x2], f3[x3]}

Which is equivalent to using

(Composition @@ MapThread[MapAt, {{f1, f2, f3}, Range[3]}])[{x1, x2, x3}]

Or

MapIndexed[{f1, f2, f3}[[First@#2]]@#1 &, {x1, x2, x3}]

$\ ${f1[x1], f2[x2], f3[x3]}


Or

#1[#2] & @@@ Thread[{{f1, f2, f3}, {x1, x2, x3}}]

$\ ${f1[x1], f2[x2], f3[x3]}

Which is similar to

#1[#2] & @@@ Thread[{f1, f2, f3} -> {x1, x2, x3}]

and

Thread[{f1, f2, f3} -> {x1, x2, x3}] /. Rule -> (#1[#2] &)

Or

{f1, f2, f3}~Function[, #1[#2], Listable]~{x1, x2, x3}

$\ ${f1[x1], f2[x2], f3[x3]}

$\endgroup$
2
  • 2
    $\begingroup$ Thank you for introducing MultiMapAt +1 :) $\endgroup$
    – ubpdqn
    Commented Nov 7, 2015 at 6:25
  • $\begingroup$ Lots on interesting solutions here. Thanks. $\endgroup$
    – Edmund
    Commented Nov 7, 2015 at 14:16
2
$\begingroup$
f = {f1, f2, f3};
x = {x1, x2, x3};

Block[{i = 0}, List @@ (f /. s_Symbol :> s[x[[i++]]])]

{f1[x1], f2[x2], f3[x3]}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.