# Is there a way to do this with Mathematica shorthand notation?

I'm a big fan of Mathematica's shorthand notation, things like @ and /@, which I use the most. It's really useful when I'm quickly whipping something up.

However, something has always driven me a bit nuts. If I want to first map a function to each element in a list, and then apply another function to the whole list, like this:

In:= a = {2, 8, 5};
SecondFn@FirstFn /@ a

Out= {SecondFn[FirstFn], SecondFn[FirstFn],
SecondFn[FirstFn]}


You can see that it's first applying SecondFn to FirstFn, and then mapping that combination of functions to each element. I get that by default @ apparently takes precedent in the order of operations over /@, but is there an easy way to make it still go from "right to left" without using parentheses?

For example, I know I can do this to get the result I want:

In:= a = {2, 8, 5};
SecondFn@(FirstFn /@ a)

Out= SecondFn[{FirstFn, FirstFn, FirstFn}]


But at that point I have to put a pair of parentheses, at which point I might as well just use brackets like normal (in fact it contains the extra symbol @).

Is there a way to do this? thanks!

You could use the operator form of Map instead:

SecondFn @ Map[FirstFn] @ {2, 3, 5}


SecondFn[{FirstFn, FirstFn, FirstFn}]

or

comp = SecondFn @* Map[FirstFn];
comp @ {2, 3, 5}


SecondFn[{FirstFn, FirstFn, FirstFn}]

• Nice, that application of the operator form of Map! (Your second solution can also be written in one line as SecondFn @* Map[FirstFn] @ {2, 3, 5}.) – Jules Lamers Aug 22 '17 at 14:47
• Thank you, this first one is exactly what I'm going for here. "right to left" and no enclosing brackets/parens. – YungHummmma Aug 22 '17 at 14:54
FirstFn /@ a // SecondFn

SecondFn[{FirstFn, FirstFn, FirstFn}]

• Hi, thanks for the response, and this is nice but still has a downside for me: it's not going "right to left". It seems like if I strung together a bunch of maps and functions I'd be bouncing around between left and right. – YungHummmma Aug 22 '17 at 14:53

A solution using only prefix notation is

List /* f @@ g /@ {x,y,z}
(* Out: f[{g[x],g[y],g[z]}] *)


Edit. A slightly nicer version (cf the order of the functions) uses Composition (@*) instead of the above RightComposition (/*):

f @* List @@ g /@ {x,y,z}
(* Out: f[{g[x],g[y],g[z]}] *)


This is still less elegant than the (rightfully) accepted answer, but it has the benefit of allowing one to use any function rather than just List.

f2 @ Function[, f1 @ #, Listable] @ {1, 2, 3}


f2[{f1, f1, f1}]

We can also use SetAttributes

SetAttributes[a, Listable]
a[x_] := b @ x

c @ a @ {1, 2, 3}


c[{b, b, b}]

• Why/how does this work? I can see that it does, even though Mma "complains" about the empty first argument of Function, and the missing & in combination with the #, by highlighting the first comma and the # in pink in my input. – Jules Lamers Aug 22 '17 at 16:21
• If the highlighting disturbes you try f2@Function[Null, f1@Slot@1, Listable]@{1, 2, 3} – eldo Aug 22 '17 at 16:25