# Simple Macro or Meta Programming for Data Analysis [duplicate]

I have a large number of data variables that are represented in MMA as symbols set to lists. For example:

x = {1, 2, 3};
y = {4, 5, 6};


I want to be able to write simple transformation rules on the data that I'd like to apply to the variables without writing the code out for each variable. For example, instead of

x = f[x];
y = f[y];


I'd like to write:

 vars = {x, y};
(# = f[#])& /@ vars;


Or, if vars was defined too late (and x and y are already set to the actual data lists) then:

(# = f[#])& /@ {x, y};


Of course neither of these approaches work because x,y are evaluated. I've tried various combinations of Hold applied to the variable list vars but have been unable to get anything to work. Is something like this even possible? Or, is there a MMA way of doing this?

## marked as duplicate by J. M. is away♦Jun 15 '13 at 2:52

• Take a look at Attributes. In particular HoldFirst – Dr. belisarius Jun 14 '13 at 21:50
• To adapt the solution in the other thread to your situation: Scan[Function[p, p = f /@ p, HoldAll], Hold[x, y]] – J. M. is away Jun 15 '13 at 2:53
• The answers to the referenced duplicate are brilliant, many thanks to 0x4A4D. – pjc42 Jun 15 '13 at 17:17

Following belisarius comment you could do something like

Function[z, z = f /@ z, HoldFirst]@{x, y}


So you put the Map operation inside the pure function. E.g.

x = {1, 2, 3};
y = {4, 5, 6};
f = #1 + 1 &;
Function[z, z = f /@ z, HoldFirst][{x, y}];
x
y


seems to work.

• Thanks, had not seen attributes applied to pure functions before. This may be my answer but before closing I wanted to ask if there is a way to use my definition of vars, i.e. Function[z, z = f /@ z, HoldFirst][vars]; This is the most convenient way and where all this started for me. Thanks. – pjc42 Jun 14 '13 at 23:50
• The question on using vars is answered the referred duplicate above. – pjc42 Jun 15 '13 at 17:48