# Through does not work with Subtract

Through[(Max - Min)[#]] & @ {1, 2, 3, 4, 5}


gives

5 + (-Min)[{1, 2, 3, 4, 5}]


Of course I can work around by

(Max[#] - Min[#]) & @ {1, 2, 3, 4, 5}


to achieve the desired result

4


But I wonder why Through does not work with Subtract and if there is a direct way to use it.

Edit:

After reading the answers, I see the problem is actually from Times failing to play the role of an operator.

Subtract[Max, Min] // FullForm


gives

Plus[Max, Times[-1, Min]]


Here Times takes a number -1 and a function Min as arguments and is expected to return a function just like what an operator should do, which unfortunately is not the case.

To solve by doing the operator's job manually:

Composition[Times[-1, #] &, Min]


and for the example:

Through[Plus[Max, Composition[Times[-1, #] &, Min]][{1, 2, 3, 4, 5}]]

• Compare: Max + Min // FullForm and Max - Min // FullForm
– Kuba
Oct 9, 2015 at 12:03

As Kuba pointed out, the issue here is that Mathematica doesn't do subtraction. From the documentation on Subtract

x-y is converted to x+(-1*y) on input.

And Through only applies the operators at the top level. So

Through[(Max*Min)[{1, 2, 3, 4, 5}]]


and

Through[(Max + Min)[{1, 2, 3, 4, 5}]]


give 5 and 6, respectively, because their FullForm expressions are Times[Max,Min] and Plus[Max,Min].

Through[(Max - Min)[{1, 2, 3, 4, 5}]]


tries to apply Times[-1,Min] to the arguement, but it is not a function. So each element at the top level has to be a function. Simple enough to solve:

Through[(Max + (-Min@# &))[{1, 2, 3, 4, 5}]]


gives the expected answer of 4.

• Or for this specific case {i.e., use of Min and Max), -Subtract @@ MinMax[{1, 2, 3, 4, 5}] (note that MinMax is new in v10.1) Oct 9, 2015 at 13:29

Another possibility

Subtract @@ Through[{Max, Min}[{1, 2, 3, 4, 5}]]


4

Here Through returns {5, 1} and then Subtract finished the job

Since the problem has its origins in how Mathematica performs subtractions, just don't use it till the very end:

Through[s[Max, Min][{1, 2, 3, 4, 5}]] /. s -> Subtract
(* 4 *)