Skip to main content
added 2 characters in body
Source Link
Murta
  • 26.5k
  • 6
  • 78
  • 172

Here is another way.

SetAttributes[set,HoldFirst] set[symb_[idx__][[part__]],var_]:=Module[{x=symb[idx]}, x[[part]]=var; symb[idx]=x ] use[symb_]:=Module[{}, Unprotect[Part]; Set[symb[idx__][[part__]], vv_]var_] ^:= set[symb[idx][[part]],vv];var]; Protect[Part]; ] 

testing with:

use[a] a[x] = {1, 2, 3} a[x][[2]] = 7 

we get, as in @Mr.Wizard test:

{1, 7, 3}

I like the fact that this solution keep the Downvalues in a, instead of a random named Global variable x. I don't like to Unprotect Part, but in this case doesn't seem to be bad.

In time performance this is not a good answer when compared with @Leonid and @Mr.Wizard.

Here is another way.

SetAttributes[set,HoldFirst] set[symb_[idx__][[part__]],var_]:=Module[{x=symb[idx]}, x[[part]]=var; symb[idx]=x ] use[symb_]:=Module[{}, Unprotect[Part]; Set[symb[idx__][[part__]], vv_] ^:= set[symb[idx][[part]],vv]; Protect[Part]; ] 

testing with:

use[a] a[x] = {1, 2, 3} a[x][[2]] = 7 

we get, as in @Mr.Wizard test:

{1, 7, 3}

I like the fact that this solution keep the Downvalues in a, instead of a random named Global variable x. I don't like to Unprotect Part, but in this case doesn't seem to be bad.

In time performance this is not a good answer when compared with @Leonid and @Mr.Wizard.

Here is another way.

SetAttributes[set,HoldFirst] set[symb_[idx__][[part__]],var_]:=Module[{x=symb[idx]}, x[[part]]=var; symb[idx]=x ] use[symb_]:=Module[{}, Unprotect[Part]; Set[symb[idx__][[part__]], var_] ^:= set[symb[idx][[part]],var]; Protect[Part]; ] 

testing with:

use[a] a[x] = {1, 2, 3} a[x][[2]] = 7 

we get, as in @Mr.Wizard test:

{1, 7, 3}

I like the fact that this solution keep the Downvalues in a, instead of a random named Global variable x. I don't like to Unprotect Part, but in this case doesn't seem to be bad.

In time performance this is not a good answer when compared with @Leonid and @Mr.Wizard.

added 92 characters in body
Source Link
Murta
  • 26.5k
  • 6
  • 78
  • 172

Here is another way.

SetAttributes[set,HoldFirst] set[symb_[idx__][[part__]],var_]:=Module[{x=symb[idx]}, x[[part]]=var; symb[idx]=x ] use[symb_]:=Module[{}, Unprotect[Part]; Set[symb[idx__][[part__]], vv_] ^:= set[symb[idx][[part]],vv]; Protect[Part]; ] 

testing with:

use[a] a[x] = {1, 2, 3} a[x][[2]] = 7 

we get, as in @Mr.Wizard test:

{1, 7, 3}

I like the fact that this solution keep the Downvalues in a, instead of a random named Global variable x. I don't like to Unprotect Part, but in this case doesn't seem to be bad.

In time performance this is not a good answer when compared with @Leonid and @Mr.Wizard.

Here is another way.

SetAttributes[set,HoldFirst] set[symb_[idx__][[part__]],var_]:=Module[{x=symb[idx]}, x[[part]]=var; symb[idx]=x ] use[symb_]:=Module[{}, Unprotect[Part]; Set[symb[idx__][[part__]], vv_] ^:= set[symb[idx][[part]],vv]; Protect[Part]; ] 

testing with:

use[a] a[x] = {1, 2, 3} a[x][[2]] = 7 

we get, as in @Mr.Wizard test:

{1, 7, 3}

I like the fact that this solution keep the Downvalues in a, instead of a random named Global variable x. I don't like to Unprotect Part, but in this case doesn't seem to be bad.

Here is another way.

SetAttributes[set,HoldFirst] set[symb_[idx__][[part__]],var_]:=Module[{x=symb[idx]}, x[[part]]=var; symb[idx]=x ] use[symb_]:=Module[{}, Unprotect[Part]; Set[symb[idx__][[part__]], vv_] ^:= set[symb[idx][[part]],vv]; Protect[Part]; ] 

testing with:

use[a] a[x] = {1, 2, 3} a[x][[2]] = 7 

we get, as in @Mr.Wizard test:

{1, 7, 3}

I like the fact that this solution keep the Downvalues in a, instead of a random named Global variable x. I don't like to Unprotect Part, but in this case doesn't seem to be bad.

In time performance this is not a good answer when compared with @Leonid and @Mr.Wizard.

Source Link
Murta
  • 26.5k
  • 6
  • 78
  • 172

Here is another way.

SetAttributes[set,HoldFirst] set[symb_[idx__][[part__]],var_]:=Module[{x=symb[idx]}, x[[part]]=var; symb[idx]=x ] use[symb_]:=Module[{}, Unprotect[Part]; Set[symb[idx__][[part__]], vv_] ^:= set[symb[idx][[part]],vv]; Protect[Part]; ] 

testing with:

use[a] a[x] = {1, 2, 3} a[x][[2]] = 7 

we get, as in @Mr.Wizard test:

{1, 7, 3}

I like the fact that this solution keep the Downvalues in a, instead of a random named Global variable x. I don't like to Unprotect Part, but in this case doesn't seem to be bad.