Skip to main content
Post Closed as "Duplicate" by Mr.Wizard
edited tags
Link
Leonid Shifrin
  • 115.8k
  • 16
  • 341
  • 435
added 2 characters in body
Source Link
phil
  • 113
  • 5

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[nestedListbfunc[nestedList_, col]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

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

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

added 565 characters in body
Source Link
phil
  • 113
  • 5

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

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

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

Source Link
phil
  • 113
  • 5
Loading