13
$\begingroup$

Assume that this built-in function is called func. What I want is

func[{f,g},{a,b,c}]

will return the following result:

{{f[a],f[b],f[c]},{g[a],g[b],g[c]}}

I can achieve this by defining a function myself:

myfunc[functions_List, data_] := 
    Table[Map[fn, data], {fn, functions}];

But is there some built-in function to achieve the same result?

(I have to admit that, after months intermittent reading of The Mathematica Book my knowledge is still very limited compared with what exist unknown in the sea.)

$\endgroup$
1

4 Answers 4

12
$\begingroup$

Update

Well I guess I should retire for the evening to a less brain-intensive activity as apparently I can't think clearly. One could of course use Outer:

Outer[Compose, {f, g}, {a, b, c}]
{{f[a], f[b], f[c]}, {g[a], g[b], g[c]}}

However I recommend that you do not do this as you will not gain the auto-compilation of Map, meaning this method will often be slower. Please see Leonid's explanation of this issue.

Old, half-awake answer

As far as I can remember there is no function that does specifically this in one step. I have used a more terse version of your own solution myself:

myMap[fns_, data_] := # /@ data & /@ fns

myMap[{f, g}, {a, b, c}]
{{f[a], f[b], f[c]}, {g[a], g[b], g[c]}}

There are of course many alternatives, e.g.:

Thread /@ Through[{f, g}[{a, b, c}]]
{{f[a], f[b], f[c]}, {g[a], g[b], g[c]}}

However this inferior because it first evaluates to:

{f[{a, b, c}], g[{a, b, c}]}

Which means that f and g may evaluate before the operation is complete. One could add an Unevaluated, Hold, Inactive etc., but that seems like pointless complexity.

Just for fun we could make use of the operator form of Map in version 10:

Through[(Map /@ {f, g})[{a, b, c}]]
{{f[a], f[b], f[c]}, {g[a], g[b], g[c]}}
$\endgroup$
3
  • $\begingroup$ Thanks, they are very helpful informations! $\endgroup$
    – Naitree
    Commented Aug 23, 2014 at 7:43
  • $\begingroup$ Compose becomes superseded by Composition, which gives f*@a instead of f[a]. Hence a bit concerned when MMA will completely remove Compose... $\endgroup$
    – xiaohuamao
    Commented Mar 20, 2018 at 0:46
  • 1
    $\begingroup$ @xiaohuamiao I have no special insight into the situation but others more knowledgeable than I (e.g. Leonid Shifrin) have indicated that it is unlikely that such functions will ever be removed. If it were to happen one could define something like Compose = #[#2] & in an initialization file to keep compatibility with existing code. $\endgroup$
    – Mr.Wizard
    Commented Mar 22, 2018 at 8:24
6
$\begingroup$

I don't think there's any built-in function to do this, but here is an alternative:

myFunc[func_, data_] := Transpose[Through[func[#]] & /@ data]

Then

myFunc[{f, g, h}, {a, b, c}]

{{f[a], f[b], f[c]}, {g[a], g[b], g[c]}, {h[a], h[b], h[c]}}

$\endgroup$
0
4
$\begingroup$

Using Query

Query[Transpose, {f, g}] @ {a, b, c}

{{f[a], f[b], f[c]}, {g[a], g[b], g[c]}}

$\endgroup$
3
$\begingroup$

Outer[#1 @ #2 &, {f, g}, {a, b, c}] or Outer[Construct, {f, g}, {a, b, c}] can do.

$\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.