# How to pass arguments and options of a function when using Map?

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]?

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]}]


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]


Which is equivalent to

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


% === %%

True

• 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. – Jules Manson Jul 7 '20 at 5:25
• ???? /@ is Map. – Bob Hanlon Jul 7 '20 at 5:30
• 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. – Jules Manson Jul 7 '20 at 5:44
• 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). – Bob Hanlon Jul 7 '20 at 5:51
• 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? – Jules Manson Jul 7 '20 at 7:21

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}]]


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}]]


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.

• 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? – Jules Manson Jul 7 '20 at 8:43
• @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. – Sjoerd Smit Jul 7 '20 at 9:07
• @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]`? – Jules Manson Jul 7 '20 at 18:17
• Yes, no problem. For example, Map[Highlighted[#, Background -> LightRed, BaseStyle -> {Red, 24}] &, list]. – FredrikD Jul 8 '20 at 10:55