Skip to main content

Timeline for How to Set parts of indexed lists?

Current License: CC BY-SA 3.0

17 events
when toggle format what by license comment
May 16, 2014 at 22:44 comment added Leonid Shifrin @AndrewStewart You've discovered a couple of subtle bugs. I fixed them, should work now (use the updated code). Use as ClearAll[a];makeReference[a]; ClearAll[i]; For[i = 1, i <= 2, i++,withModifiedPart[ a[i] = {i, i ** 2, i ** 3};Print[a[i]];a[i][[1]] = 5; a[i]]]. Thanks for bringing this up.
May 16, 2014 at 22:43 history edited Leonid Shifrin CC BY-SA 3.0
Fixed two subtle bugs
May 16, 2014 at 22:22 comment added Andrew Stewart I can't seem to get this working inside a For loop, ie. I have something like For[i=1, i<=2, i++, withModifiedPart[a[i]={i,i2,i3};a[i][[1]]=5;a[i]] (or something along those lines. Am I doing something wrong?
Jun 22, 2012 at 22:50 history edited Leonid Shifrin CC BY-SA 3.0
Removed an erroneous statement that Part holds its first arg
Jun 22, 2012 at 19:04 comment added Leonid Shifrin @Rojo Yes, that's a good catch. You exploit that the rule does not match during the pattern-matching stage, and the symbol gets substituted as a part of Set evaluation semantics (it evaluates heads).
Jun 22, 2012 at 18:13 comment added Rojo @LeonidShifrin, nice one, and that could also allow me to remove the $guardOver from my version and make it more readable. Out of curiosity, Set doesn't seem to be recursive, so another alternative for cases where you've already messed with SetDelayed is something like #&[sym][index] = ...
Jun 22, 2012 at 17:49 comment added Leonid Shifrin @Rojo I could also have used SetDelayed instead of appending to DownValues explicitly.
Jun 22, 2012 at 15:46 comment added Rojo Leonid, this got me thinking about how I would have done it. Posted it as an answer because the code got long for a comment, with the appropriate warnings, and an extra idea to ease my conscience on posting it as an answer
Jun 22, 2012 at 14:12 comment added Leonid Shifrin @Rojo Yes, you are right, that as well. I though about that as well, but indicated the above as a primary reason.
Jun 22, 2012 at 14:07 comment added Rojo Right, and furthermore you avoid creating a different symbol every time a certain index is reassigned a new value, leaking memory. My yet-non-DownValues-user instinct would have led me to tackle both issues differently. Using explicit DownValues is something that doesn't come straight to mind
Jun 22, 2012 at 13:54 comment added Leonid Shifrin @Rojo Thanks:). As to the reason for my construct: because I already overload Set on this symbol, I can not use it (Set) to set the DownValue, since the new definition will be used, and we will enter an infinite recursion.
Jun 22, 2012 at 13:52 comment added Rojo +1, nice one! What's the purpose of setting the DownValue by hand only when there's no other downvalue associated with the symbol, versus simply sym[index]=ref?
Jun 22, 2012 at 13:51 vote accept gwr
Jun 22, 2012 at 13:50 comment added Leonid Shifrin @gwr Thanks :). In fact, since Part is overloaded via UpValues, and then only for Set, and part assignments for indexed symbols are normally prohibited anyway, I would even think that globally overloading Part could be ok (i.e. doing the same thing but without the dynamic invironment), in which case, you don't have to wrap your code in it. But, it is still better to be on the safe side, all it takes is just to wrap your top-level function(s) in withModifiedPart. Thanks for the accept as well.
Jun 22, 2012 at 13:47 comment added gwr Leonid, what a solution, thank you. I had believed there is some 'easy' way - possibly upvalues - but your answer shows how deep the matter runs.
Jun 22, 2012 at 13:29 history edited Leonid Shifrin CC BY-SA 3.0
added 201 characters in body
Jun 22, 2012 at 13:23 history answered Leonid Shifrin CC BY-SA 3.0