Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Replace parts of an expression using replacement rules and a part specification
ResourceFunction["ReplaceAt"][expr,rules,n] transforms expr by replacing the nth element using rules. | |
ResourceFunction["ReplaceAt"][expr,rules,{i,j,…}] replaces the part of expr at position {i,j,…}. | |
ResourceFunction["ReplaceAt"][expr,rules,{{i1,j1,…},{i2,j2,…},…}] replaces parts at several positions. | |
ResourceFunction["ReplaceAt"][rules,pos] represents an operator form of ResourceFunction["ReplaceAt"] that can be applied to an expression. |
Replace a specified part of an expression using a rule:
| In[1]:= |
| Out[1]= |
Replace multiple parts:
| In[2]:= |
| Out[2]= |
Replace a nested part:
| In[3]:= |
| Out[3]= |
Use negative positions to count from the end:
| In[4]:= |
| Out[4]= |
| In[5]:= |
| Out[5]= |
Use pattern-based replacements:
| In[6]:= |
| Out[6]= |
Use multiple replacement rules:
| In[7]:= |
| Out[7]= |
Replace the second element of all top-level parts (the “second column”):
| In[8]:= |
| Out[8]= |
Replace parts of an association specified by Key:
| In[9]:= |
| Out[9]= |
| In[10]:= |
| Out[10]= |
Use the operator form of ReplaceAt:
| In[11]:= |
| Out[11]= |
A single element of the expression:
| In[12]:= |
| Out[12]= |
Negative numbers count from the end:
| In[13]:= |
| Out[13]= |
Replace parts at level 2:
| In[14]:= |
| Out[14]= |
Replace several parts:
| In[15]:= |
| Out[15]= |
Replace several nested parts:
| In[16]:= |
| Out[16]= |
Use Span syntax:
| In[17]:= |
| Out[17]= |
ReplaceAt can be used on expressions with any head:
| In[18]:= |
| Out[18]= |
| In[19]:= |
| Out[19]= |
Key and integer part specifications in associations are different:
| In[20]:= |
| Out[20]= |
Use part 0 to perform replacements in the head of an expression:
| In[21]:= |
| Out[21]= |
ReplaceAt can be used with sparse arrays:
| In[22]:= |
| Out[22]= |
| In[23]:= |
| Out[23]= |
ReplaceAt can use lists of parts returned by Position:
| In[24]:= |
| Out[24]= |
| In[25]:= |
| Out[25]= |
| In[26]:= |
| Out[26]= |
These are the subexpressions returned by Extract:
| In[27]:= |
| Out[27]= |
ReplaceAt can use an Association for replacement rules:
| In[28]:= |
| Out[28]= |
| In[29]:= |
| Out[29]= |
ReplaceAt can use a Dispatch object for replacement rules:
| In[30]:= |
| Out[30]= |
| In[31]:= |
| Out[31]= |
ReplaceAt applies replacement rules repeatedly if a part is mentioned repeatedly:
| In[32]:= |
| Out[32]= |
ReplaceAt will not evaluate held replacements:
| In[33]:= |
| Out[33]= |
Manipulate code in an unevaluated association:
| In[34]:= |
| Out[34]= |
| In[35]:= |
| Out[35]= |
Evaluate upon retrieval:
| In[36]:= |
| Out[36]= |
Use Unevaluated to perform replacements prior to evaluating expr:
| In[37]:= |
| Out[37]= |
Repeated replacement parts may produce unexpected results if the structure of the expression is expected to change during replacements:
| In[38]:= |
| Out[38]= |
Compare to using Nest:
| In[39]:= |
| Out[39]= |
This behavior is consistent with ReplacePart and MapAt:
| In[40]:= |
| Out[40]= |
| In[41]:= |
| Out[41]= |
Modify the resource functions in a piece of code before running it:
| In[42]:= | ![]() |
| Out[43]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License