8
$\begingroup$

is there any way to use the Apply[] function to a sum of objects? By this I mean the following. We know that using Apply[] (@@) to an objects gives:

f@@{a}
f[a]

I'm wondering if there's a way of doing something like

(f+g)@@{a}
f[a]+g[a]

Does anyone knows anything about it? Thanks!

$\endgroup$
4
  • 9
    $\begingroup$ Perhaps you require Through[Sequence@@@(f + g)@{a}] or Through[(f + g)[a]]? (or Sequence@@@(f + g)@{a}//Through) $\endgroup$
    – user1066
    Sep 24, 2021 at 22:18
  • 7
    $\begingroup$ #@a & /@ (f + g) $\endgroup$
    – Bob Hanlon
    Sep 25, 2021 at 2:34
  • 4
    $\begingroup$ Also Through[(f + g)[a]] or Through[(f + g) @@ {a}] $\endgroup$
    – Ben Izd
    Sep 25, 2021 at 7:38
  • $\begingroup$ Thanks! All these work! $\endgroup$
    – Einj
    Sep 25, 2021 at 23:13

2 Answers 2

5
$\begingroup$

As of version 14.0, ComapApply is introduced:

ComapApply[f + g, {a}]
(* f[a] + g[a] *)

ComapApply[f + g]@{a}
(* f[a] + g[a] *)

enter image description here

BTW, if the object isn't a List {}, you can directly use Comap (also new in v14):

Comap[f + g, a]
(* f[a] + g[a] *)

Comap[f + g]@a
(* f[a] + g[a] *)
$\endgroup$
4
$\begingroup$

This is another way and an active effort to advertise the ThroughOperator that was developed by @Sjoerd Smit. This is rather new and I find it convenient.

To the extend of my knowledge it was first suggested in this answer.

fnctns = {f, g};
oprtr = ResourceFunction["ThroughOperator"][fnctns, Plus];

and then

oprtr /@ {a} /. List -> (# &)

res

$\endgroup$
7
  • $\begingroup$ Does this accomplish anything that Though[(f+g)[a]] doesn't? Why should we rely on an external solution when an internal one exists? $\endgroup$ Jan 17, 2023 at 8:22
  • $\begingroup$ @NajibIdrissi did you visit any of the links I provided? I am asking out of curiosity $\endgroup$
    – bmf
    Jan 17, 2023 at 8:25
  • $\begingroup$ Yes, I visited the links. $\endgroup$ Jan 17, 2023 at 8:36
  • 1
    $\begingroup$ @NajibIdrissi in the second link there's this explanation in a comment Through[{f1, f2, f3}[5]] is annoying to map over lists, which is one of the main motivations I made ThroughOperator. Another reason is that Through[h[f1, f2][x]] sometimes suffers from order-of-evaluation issues that are difficult to circumvent which for me at least was enough to give it a go. Also, I just suggested this an alternative. I realize what the easiest way is. $\endgroup$
    – bmf
    Jan 17, 2023 at 8:39
  • 3
    $\begingroup$ Glad to see my function is being used :) $\endgroup$ Jan 17, 2023 at 12:57

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.