44

Say I have a group of functions:

f1[a_] := a * -1;
f2[a_] := a * 100;
f3[a_] := a / 10.0;

and some data in a list:

data := Range[1, 20];

I would like to apply this group of functions to the data: the first function applied to the first item of data, the second to the second, and so on. Because there are more data elements than there are functions, the first function is also applied to the fourth data element, and so on.

A simple work-round is this:

Flatten[{f1[#[[1]]], f2[#[[2]]], f3[#[[3]]]}  & /@ Partition[data, 3]]

giving

{-1, 200, 0.3, -4, 500, 0.6, -7, 800, 0.9, -10, 1100, 1.2, -13, 1400, 
  1.5, -16, 1700, 1.8}

but this isn't an ideal solution: the slots have been 'hard-wired', and it wouldn't be possible to modify the list of functions easily.

Is there a Map-related function that could do this elegantly? I've not been able to discover it yet.

(This is a toy example, of course!)

rm -rf
  • 88,781
  • 21
  • 293
  • 472
cormullion
  • 24,243
  • 4
  • 64
  • 133

8 Answers8

32

Use PadRight with the cyclical padding setup:

funcs = {f1, f2, f3};
data = Range[1, 20];
MapThread[#1[#2] &, {PadRight[funcs, Length@data, funcs], data}]

or

MapThread[Compose, {PadRight[funcs, Length@data, funcs], data}]

{f1[1], f2[2], f3[3], f1[4], f2[5], f3[6], f1[7], f2[8], f3[9], f1[10], f2[11], f3[12], f1[13], f2[14], f3[15], f1[16], f2[17], f3[18], f1[19], f2[20]}

István Zachar
  • 47,032
  • 20
  • 143
  • 291
27

Here's how you can do it in a simple way:

functionMap[funcs_List, data_] := Module[{fn = RotateRight[funcs]}, 
    First[(fn = RotateLeft[fn])][#] & /@ data]

Use it as:

functionMap[{f1, f2, f3}, Range[20]]
(* {f1[1], f2[2], f3[3], f1[4], f2[5], f3[6], f1[7], f2[8], f3[9], f1[10],
    f2[11], f3[12], f1[13], f2[14], f3[15], f1[16], f2[17], f3[18], f1[19], f2[20]} *)
rm -rf
  • 88,781
  • 21
  • 293
  • 472
21
MapIndexed[{f1, f2, f3}[[Mod[First@#2, 3, 1]]][#1] &, data]

does what you want (thanks to Sjoerd for pointing out a silly inefficiency).

acl
  • 19,834
  • 3
  • 66
  • 91
  • 1
    This is more or less what I was thinking about too, but I wouldn't give the arguments to the functions first. In this way you're doing 3 times more calculations than necessary. Just put a [#1] after the Part and leave the function list alone. – Sjoerd C. de Vries Apr 03 '12 at 10:44
  • @Sjoerd yes good point, also, this way it generalizes better. thanks – acl Apr 03 '12 at 10:58
20

Another approach

#1@#2 & @@@ 
  Partition[Riffle[Range[20], {f1, f2, f3}, {1, -1, 2}], 2] 

Comparing with Acl's solution:

#1@#2 & @@@ 
  Partition[Riffle[Range[20], {f1, f2, f3}, {1, -1, 2}], 2] == 
 MapIndexed[{f1@#1, f2@#1, f3@#1}[[Mod[First@#2, 3, 1]]] &, data

==> True

user1066
  • 17,923
  • 3
  • 31
  • 49
  • 1
    nice one +1. Can save few key strokes with #2@#1 & @@@ Partition[Riffle[Range[20], {f1, f2, f3}], 2]:) – kglr Apr 03 '12 at 14:02
17

Another approach using Fold in combination with Sow/Reap:

Reap[Fold[(Sow[#[[1]][#2]]; RotateLeft[#]) &, {f1, f2, f3}, data];][[2, 1]]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Heike
  • 35,858
  • 3
  • 108
  • 157
17

Another approach is to use Outer, as follows

Flatten@Outer[#1[#2]&, {f1, f2, f3}, Range[1,5]]
(*
{f1[1], f1[2], f1[3], f1[4], f1[5], 
 f2[1], f2[2], f2[3], f2[4], f2[5],
 f3[1], f3[2], f3[3], f3[4], f3[5]}
*)

Unfortunately, Outer causes problems if the data points are vectors, for instance

Flatten@Outer[#1[#2] &, {f1, f2, f3}, {#, #} & /@ Range[1, 3]]

produces

{f1[1], f1[1], f1[2], f1[2], f1[3], f1[3], 
 f2[1], f2[1], f2[2], f2[2], f2[3], f2[3], 
 f3[1], f3[1], f3[2], f3[2], f3[3], f3[3]}

To work around this, you need to use the 3$^\text{rd}$ and subsequent parameters of Outer which are level specifications:

Outer[#1[#2] &, {f1, f2, f3}, {#, #} & /@ Range[1, 3], Infinity, 1] //
  Flatten
(*
{f1[{1, 1}], f1[{2, 2}], f1[{3, 3}], 
 f2[{1, 1}], f2[{2, 2}], f2[{3, 3}], 
 f3[{1, 1}], f3[{2, 2}], f3[{3, 3}]}
*)

Or, if you prefer

Outer[#1 @@ #2 &, {f1, f2, f3}, {#, #} & /@ Range[1, 3], Infinity, 1] //
  Flatten
(*
{f1[1, 1], f1[2, 2], f1[3, 3], 
 f2[1, 1], f2[2, 2], f2[3, 3], 
 f3[1, 1], f3[2, 2], f3[3, 3]}
*)
rcollyer
  • 33,976
  • 7
  • 92
  • 191
8

Would the following qualify as elegant or not?

mapFunctions[funcs_, list_] := Module[{r = list, u},
  applyFunc[f_, l_, i_, t_] := 
   MapAt[f, l, Table[{u}, {u, i, Length[l], t}]];
  Do[r = applyFunc[funcs[[u]], r, u, Length[funcs]],
   {u, Length[funcs]}];
  Return[r];
  ]

which gives:

In[9]:= MapFunctions[{f1, f2, f3}, data]

Out[9]= {f1[1], f2[2], f3[3], f1[4], f2[5], f3[6], f1[7], f2[8], 
 f3[9], f1[10], f2[11], f3[12], f1[13], f2[14], f3[15], f1[16], 
 f2[17], f3[18], f1[19], f2[20]}
F'x
  • 10,817
  • 3
  • 52
  • 92
1

Using Query

f1[a_] := a * -1;
f2[a_] := a * 100;
f3[a_] := a / 10.0;

data = Partition[Range @ 18, 3]

Query[All, {1 -> f1, 2 -> f2, 3 -> f3}] @ data

{{-1, 200, 0.3}, {-4, 500, 0.6}, {-7, 800, 0.9}, {-10, 1100, 1.2}, {-13, 1400, 1.5}, {-16, 1700, 1.8}}

In a simple case like this we don't need the f - definitions:

Query[All, Thread[{1, 2, 3} -> {# * -1 &, # * 100 &, # / 10. &}]] @ data

This gives, of course, the same result.

eldo
  • 67,911
  • 5
  • 60
  • 168