8
$\begingroup$

For example how can I write

In:
pureFunctionSyntax[myF] /@ {7, 3}
Out:
{myF[7, Log[7]], myF[3, Log[3]]}
$\endgroup$
1

3 Answers 3

11
$\begingroup$

Imo the most common/readable/flexible way:

Function[h, h[#, Log[#]] &][myF] /@ {7, 3}

and for fun, less general, as pointed in comments:

Through@*#[Identity, Log] &[myF] /@ {7, 3}

which can be even shorter, thanks to ybeltukov

Through@*#[# &, Log] &[myF] /@ {7, 3}
$\endgroup$
5
  • 1
    $\begingroup$ Wow, you can write even shorter Through@*#[# &, Log] &. $\endgroup$
    – ybeltukov
    Nov 18, 2015 at 17:10
  • $\begingroup$ @ybeltukov ah, of course, thanks :) $\endgroup$
    – Kuba
    Nov 18, 2015 at 17:14
  • 1
    $\begingroup$ This wont work if myF has a definition. For example Through@*#[Identity, Log] &[f[#1] + #2 &] /@ {7, 3} outputs {Log[7] + f[Identity][7], Log[3] + f[Identity][3]} not {f[7] + Log[7], f[3] + Log[3]}. $\endgroup$
    – user
    Nov 18, 2015 at 21:19
  • $\begingroup$ @user This is one of reasons why I've said it is less general, but the first one will work anyway. $\endgroup$
    – Kuba
    Nov 18, 2015 at 22:08
  • $\begingroup$ @user I've edited the question to stress out your concerns. $\endgroup$
    – Kuba
    Nov 18, 2015 at 22:41
10
$\begingroup$

Yes, you can use only pure functions:

f = ## &[#, Log@#] & /* # &;

f[myF] /@ {7, 3}
(* {myF[7, Log[7]], myF[3, Log[3]]} *)

It can be shorter with a bit different syntax:

g = ## &[#, Log@#] &;
g /* myF /@ {7, 3}
(* {myF[7, Log[7]], myF[3, Log[3]]} *)
$\endgroup$
3
$\begingroup$

I like this syntax:

In:
f[#, Log[#]] & /. f -> # &[myF] /@ {7, 3}
Out:
{myF[7, Log[7]], myF[3, Log[3]]}
$\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.