Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Use rules to make assignments
| ResourceFunction["SetByRules"][rules] sets left sides of rules to their right sides. | |
| ResourceFunction["SetByRules"][rules,f] assigns using functionf to resolve conflicts in case of identical left hand sides. | 
Perform assignments according to given rules:
| In[1]:= | ![ResourceFunction["SetByRules"][{x -> 2, f[x_] :> x^2}]](https://www.wolframcloud.com/obj/resourcesystem/images/815/815899ac-2261-4ce6-8b48-c7bd6d49d4d0/61c26fd154e19b2e.png) | 
| Out[1]= |  | 
| In[2]:= | ![f[x]](https://www.wolframcloud.com/obj/resourcesystem/images/815/815899ac-2261-4ce6-8b48-c7bd6d49d4d0/0858d01a8ba86174.png) | 
| Out[2]= |  | 
Apply the first rule in case of conflict:
| In[3]:= | ![ResourceFunction["SetByRules"][{n -> 1, n -> 2, n -> 3}]](https://www.wolframcloud.com/obj/resourcesystem/images/815/815899ac-2261-4ce6-8b48-c7bd6d49d4d0/0fc7015da7c7d325.png) | 
| Out[3]= |  | 
| In[4]:= |  | 
| Out[4]= |  | 
Specify how to resolve a conflict:
| In[5]:= | ![ResourceFunction["SetByRules"][{n -> 1, n -> 2, n -> 3}, Last]](https://www.wolframcloud.com/obj/resourcesystem/images/815/815899ac-2261-4ce6-8b48-c7bd6d49d4d0/534a25b28074d28c.png) | 
| Out[5]= |  | 
| In[6]:= |  | 
| Out[6]= |  | 
Rules are not distinguished by pattern parameters:
| In[7]:= | ![ResourceFunction[
 "SetByRules"][{g[x_] :> x, g[1] -> -1, g[y_] :> y + 1, g[z_] /; z > 0 :> Sqrt[z]}]](https://www.wolframcloud.com/obj/resourcesystem/images/815/815899ac-2261-4ce6-8b48-c7bd6d49d4d0/5acbc6b26e3985b2.png) | 
| Out[7]= |  | 
| In[8]:= | ![Definition[g]](https://www.wolframcloud.com/obj/resourcesystem/images/815/815899ac-2261-4ce6-8b48-c7bd6d49d4d0/1dd65b3a82888e6b.png) | 
| Out[8]= |  | 
Assignment immediately after solving the equation:
| In[9]:= | ![Solve[{a^2 + b^2 == c^2, a + b + c == 1000, 0 < a < b < c}, {a, b, c},
   Integers] // ResourceFunction["SetByRules"]](https://www.wolframcloud.com/obj/resourcesystem/images/815/815899ac-2261-4ce6-8b48-c7bd6d49d4d0/3d6443ee31a7856c.png) | 
| Out[9]= |  | 
| In[10]:= |  | 
| Out[10]= |  | 
If the conflict is not resolved effectively, then a warning will be generated and the conflict will be automatically resolved:
| In[11]:= | ![ResourceFunction["SetByRules"][{n -> 1, n -> 2, n -> 3}, Identity]](https://www.wolframcloud.com/obj/resourcesystem/images/815/815899ac-2261-4ce6-8b48-c7bd6d49d4d0/081e390858bb56ba.png) | 
| Out[11]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License