I would like to apply a random color to several list elements. For example:
Style[{x, y}, RandomColor[]] makes the list itself colored. But I want the elements to be colored separately, not the list itself. If I do
(Style[#, RandomColor[]] &) /@ {x, y} only the elements get colored, but they get a different color each. Of course I can fix this by writing
rColor[x_]:=Block[{rd}, rd=RandomColor[]; (Style[#, rd] &) /@ x ] So that
rColor[{x, y}] which is the desired output. But it just does not feel right to write a whole block for such a simple function. Is there a one-line solution that produces the desired output?





randomStyle[] := With[{c = RandomColor[]}, Style[#, c] &]; randomStyle[] /@ Range[10]You can leave off the[]fromrandomStyle, but I prefer it this way because it makes it clear thatrandmoStyleevaluates once and returns something (another function). $\endgroup$Withreplacescby an actual colour within theFunction.Blockwould just give a temporary value toc, butcstill exist as a variable that needs to be evaluated to get the colour. $\endgroup$Blocktransports the entire input to a local scope, applies the operations within theBlockand returns the entire list as an output. TheWithmodifies the function itself before it is applied. $\endgroup$BlockRandom[Style[#, RandomColor[]] ]& /@ {x, y, z, w}? $\endgroup$RandomColor[]; BlockRandom[Style[#, RandomColor[]] ]& /@ {x, y,z,w}, but yourrColoris much cleaner than this. $\endgroup$