# Using several anonymous functions mixed together

This works:

list // BarChart[#[[2]], ChartLabels -> DateString @@@ #[[1]], ChartStyle -> "Pastel"] &


This doesn't:

list // BarChart[#[[2]], ChartLabels ->
DateString[#, {"ShortDay", "/", "ShortMonth"}] & @@@ #[[1]], ChartStyle -> "Pastel"] &


The difference is that in the second version DateString needs to be an anonymous function. But the entire expression already is an anonymous function. Since this won't work, how can I do what I want to do? Is the best way perhaps to define DateString[#,..] somewhere else, or can I solve this with parentheses or something like that?

-
You make the statement: "this won't work," which implies that you think it will not work, but haven't tried it. I would be surprised that it doesn't work as the parser can identify that case. – rcollyer Apr 4 '12 at 17:51
The fact is I tried it. And it didn't work. But using Function like Sjoerd suggested worked fine, I didn't realize Function[] was unambigous even if #& is not. That's a good thing to remember. – Anon Apr 4 '12 at 17:53
Hmmm, that surprises me. Thanks. – rcollyer Apr 4 '12 at 17:54
It's nothing special about Function Calle. It's the fullform (notation with [ ]) that is always explicit about operator precedence. Everything has a full form. Try FullForm[3 # &]. You'll see that it's equivalent to Function[3 #]. If you feel more confident using the full form notation, you can always do it – Rojo Apr 5 '12 at 1:17

You can always use Function to create anonymous functions:

Function[{a},a^2]


is equivalent to

#^2&


and can be used as such, but it is unambiguous.

It can be used as:

Function[{a},a^2][2]

(* ==> 4  *)

-

You can always string several anonymous functions together, but you'll also have to pay attention to operator precedence. In this case, you had to enclose the anonymous function in parentheses. Replace the corresponding line in your second example with the following and it works.

ChartLabels -> (DateString[#, {"ShortDay", "/", "ShortMonth"}] & @@@ #[[1]])


You can read this documentation page for more info on operator precedences. In your specific case, Rule or -> has a higher precedence than Function or &.

-