7
$\begingroup$

A minimum example is this:

ls = {1 + I, 2 - I};
Max@Re[ls]
Min@Im[ls]
#[ls] & /@ {Max@Re, Min@Im} (*want {2, -1}*)

The result is:

{{1, 2}, {1, -1}}

I don't know why is this..

$\endgroup$
3
  • 2
    $\begingroup$ @ does not denote function composition. It denotes function applications. f@g[x] is not (f@g)[x] but f@(g[x]), merely a different notation for f[g[x]]. $\endgroup$
    – Szabolcs
    Apr 7, 2020 at 9:41
  • $\begingroup$ You could just do {Max@Re[#], Min@Im@#} &@ls applying your function to ls instead of mapping it. $\endgroup$
    – N.J.Evans
    Apr 7, 2020 at 14:59
  • $\begingroup$ @Szabolcs But Max[Re[ls]] gives just a number, why need composite? Also Max@Re[ls] gives a number $\endgroup$ Apr 8, 2020 at 3:54

5 Answers 5

11
$\begingroup$

For a minimal change in your code, you can replace @ with Composition (@*)

#[ls] & /@ {Max@*Re, Min@*Im} 
 {2, -1}

Consider also Through:

Through @ {Max @* Re, Min @* Im} @ ls
{2, -1}

Aside: Why your code gives {{1, 2}, {1, -1}}:

Trace[#[ls] & /@ {Max@Re, Min@Im}] // Column

enter image description here

Notice that Max@Re is replaced with Re and Min@Im is replaced with Im in the very first step of evaluation.

$\endgroup$
2
  • $\begingroup$ If you can further add the reason why we need composite functions, the answer would be better. For example, why Max@Re[ls] Min@Im[ls] work as expected but not work when using Apply? $\endgroup$ Apr 8, 2020 at 3:56
  • $\begingroup$ @anoffercan'trefuse, added a note re why you get {{1, 2}, {1, -1}} if you use {Max@Re, Min@Im} instead of {Max@*Re, Min@*Im} or {Max@Re@#&, Min@Im@#&} $\endgroup$
    – kglr
    Apr 8, 2020 at 15:46
3
$\begingroup$
Map[{Max[Re[#]], Min[Im[#]]} &, ls, {0}]

Gives

{2, -1}

From help on Map

Level 0 corresponds to the whole expression.

$\endgroup$
3
$\begingroup$
ls = {1 + I, 2 - I};

Using Comap (new in 14.0)

Comap[{Max @* Re, Min @* Im}] @ ls

{2, -1}

$\endgroup$
2
$\begingroup$
ls = {1 + I, 2 - I};

Query[{Max @* Re, Min @* Im}] @ ls

{2, -1}

$\endgroup$
1
$\begingroup$
ls = {1 + I, 2 - I};

Another way using Thread:

Last@Activate@#1@#2 & @@@ Thread[{{Max@Re, Min@Im}, Inactive[ls]}]

Result:

{2, -1}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.