1
$\begingroup$

Suppose I have a list in which I need to map a function to a list. Also suppose that function takes several arguments and even more options (such as styling). How would I pass all arguments and options when mapping f to the list using Map[...]?

Below is a simple contrived example...

list=Range[10]
fn[arg1,arg2,arg3]:=Module[do something with all args, options->spec]
Map[fn,list] (* how would I pass all arguments and specify style options? *)

What if the function I wish to map is a native function (built-in) NativeFn[...] with style options? How would I pass those through the Map[NativeFn,list]?

$\endgroup$
2
$\begingroup$
Clear["Global`*"];

funcs = {(1 - x)/2, -2 - 3 x};

pt = {x, funcs[[1]]} /. Solve[
    Equal @@ funcs, x, Reals][[1]]

(* {-1, 1} *)

Adding options to the Callout

Plot[Evaluate[Callout[#, #, {Scaled[0.8], Above},
     Background -> LightGray] & /@ funcs], {x, -2, 1},
 Epilog -> {Red, AbsolutePointSize[6],
   Tooltip[Point[pt], pt]}]

enter image description here

EDIT: When asking a question provide a specific example; otherwise we are guessing what will meet your needs.

graphicsPoint[loc_ : {0, 0}, color_ : Red, size_ : 8] := 
  Block[{graphic}, graphic = If[Length[loc] == 2, Graphics, Graphics3D];
   graphic[{color, AbsolutePointSize[size], 
     Tooltip[Point[loc], loc, 
      TooltipStyle -> {22, Black, Background -> LightBlue}]}]];

list = Transpose[{RandomReal[{0, 1}, {10, 2}], RandomColor[10], 
    RandomInteger[10, 10]}];

Show[graphicsPoint @@@ list, ImageSize -> 200]

enter image description here

Which is equivalent to

Show[graphicsPoint @@ # & /@ list, ImageSize -> 200]

enter image description here

% === %%

True
$\endgroup$
6
  • $\begingroup$ I really appreciate your attention to this post but it doesn't answer the question. It doesn't address passing extra arguments and options in a Map or MapAt. $\endgroup$ – Jules Manson Jul 7 '20 at 5:25
  • $\begingroup$ ???? /@ is Map. $\endgroup$ – Bob Hanlon Jul 7 '20 at 5:30
  • $\begingroup$ oops my bad. The plots are already labeled with PlotLabels. The point of my question is to generate a label for the intersection point. That is why I wanted to apply a convenient labeling function to just one (not both) of the equations using MapAt at a specific location of interest which is the intersection point. Secondly I really don't want to clutter the Plot function too much. That is why I wanted to use MapAt for the equations outside the Plot which is why I asked about passing arguments and options when mapping functions onto lists. $\endgroup$ – Jules Manson Jul 7 '20 at 5:44
  • $\begingroup$ This example shows the pure function Callout[#, #, {Scaled[0.8], Above}, Background -> LightGray] & being mapped onto a list. You can replace that function with whatever you want (assuming that it is compatible with the context, Plot in this example). $\endgroup$ – Bob Hanlon Jul 7 '20 at 5:51
  • $\begingroup$ Thank you for your help. As you can probably tell I am quite new to Mathematica but have some experience coding (fortran, javascript, php, etc.). I asked question not just to solve how to label points along a plot but also to understand better how arguments and options are passed using Map and variants like MapAt and Apply outside any enclosing function (i.e. Plot, etc.) so that I could pass these objects around as needed. For example how to pass f[list, arg2,arg3]:=fn[do something with list and args,options->spec] when just doing a pure Map[fn,list] in global scope? $\endgroup$ – Jules Manson Jul 7 '20 at 7:21
2
$\begingroup$

Perhaps this is what you are looking for

list = Range[10]; 
fn[arg1_, arg2_, arg3_] := 
  Module[{}, 
   Return["do something w element " <> ToString[arg1] <> " given " <> 
     StringRiffle[{arg2, arg3}]]]; 
MapAt[fn[#, "a2", "a3"] &, list, RandomInteger[{1, 10}]]

enter image description here

An example with a native functions with different options to explore, Highlighted.

options = {Background -> LightRed, BaseStyle -> {Red, 24}}; 
fn[element_, options_, arg3_] := 
  Module[{}, Return[Highlighted[element, options]]];
MapAt[fn[#, options, {}] &, list, RandomInteger[{1, 10}]]

enter image description here

As Sjörd Smit writes, the options can be a list. Which means you can have a function that creates the options that fit your specific need.

$\endgroup$
4
  • 1
    $\begingroup$ this was excellent. This solves the case for passing arguments to user-defined and built-in functions. How would I pass style options for built-in functions? $\endgroup$ – Jules Manson Jul 7 '20 at 8:43
  • 1
    $\begingroup$ @JulesManson: Same way, really. The # and & you see here are short forms of Function. It's how you can define functions in-place in Mathematica. I recommend reading the documentation of Function for examples. $\endgroup$ – Sjoerd Smit Jul 7 '20 at 9:07
  • 1
    $\begingroup$ @FredrikD so if we had a native function that operates on all members of a list I could do something like this: Map[NativeFunction[#, arg2, Arg3, Frame->True, Background-> LightBlue]&, list]? $\endgroup$ – Jules Manson Jul 7 '20 at 18:17
  • $\begingroup$ Yes, no problem. For example, Map[Highlighted[#, Background -> LightRed, BaseStyle -> {Red, 24}] &, list]. $\endgroup$ – FredrikD Jul 8 '20 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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