Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Reverse a Rule or RuleDelayed expression
ResourceFunction["RuleReverse"][rule] returns rule after swapping the left- and right-hand sides, while keeping undetermined variables on the left-hand side. | |
ResourceFunction["RuleReverse"][rule,vars] forces vars as undetermined variables on the left-hand side of the output. |
Simple cases work like Reverse:
| In[3]:= |
| Out[3]= |
With variable cases, blanks are forced to stay on the left-hand side:
| In[4]:= |
| Out[4]= |
RuleReverse preserves types when possible:
| In[5]:= |
| Out[5]= |
Applying RuleReverse twice does not act as the identity when variables are projected out:
| In[6]:= |
| Out[7]= |
| In[8]:= |
| Out[8]= |
This issue can be fixed by setting the extra vars argument:
| In[9]:= |
| Out[9]= |
| In[10]:= |
| Out[10]= |
If the right-hand side evaluates, repeated application might not act as the identity:
| In[11]:= | ![]() |
| Out[11]= |
This issue can be fixed by adding a Hold to the right-hand side:
| In[12]:= | ![]() |
| Out[12]= |
This work is licensed under a Creative Commons Attribution 4.0 International License