An example will be most specific:
func[list_, column_] := list[[All, column]] = Map[#*2 &, list[[All, column]]];
This throws errors.
I want to avoid doing something like this:
func2[list_] := Map[#*2 &; list];
list[[All, 2]] = func2[list[[All,2]]]
because nesting a couple of functions raises complexity unnecessarily, the output would have to be reassigned every time.
Thanks in advance.
As a followup, using HoldFirst works fine, but using the so defined function in a Map gives again errors.
The setup is as follows:
create a nested list
testList = Table[Table[{x y, x y 2}, {x, 1,3}], {y,1,3}]
define afunc with HoldFirst Attribute
afunc = Function[{list, col}, list[[All, col]] = Map[# * 2 &, list[[All, col]]], HoldFirst]
and another function using the first
bfunc[nestedList_, col_] := Map[afunc[#, col] &, nestedList]
now, a call to
bfunc[testList, 2]
should alter the 2'nd columns of the nested lists I'd expect, but it instead throws errors
i've tried to set Attribute HoldFirst on this function as well but it didn't work out as expected
bfunc
:ClearAll[bfunc]; SetAttributes[bfunc, HoldFirst]; bfunc[nestedList_, col_] := Table[With[{i = i}, afunc[nestedList[[i]], col]], {i, Length[nestedList]}]
. Note that 1: You should have used patterns andHoldFirst
forbfunc
2:the constructionf[#]&
is not the same asf
,since it leaks evaluation during the parameter-passing in a pure function 3.Map
is a wrong tool for the task, since there is no way with it to prevent evaluation of sub-parts. – Leonid Shifrin Mar 28 '12 at 11:34