Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Transform an expression by replacing all symbols in one context with symbols of the same name in another context
| ResourceFunction["ReplaceContext"][expr,"context1`"→"context2`"] gives expr with all symbols from "context1`" replaced with symbols in "context2`". | |
| ResourceFunction["ReplaceContext"][expr,rules] replaces the context with the list of rules. | |
| ResourceFunction["ReplaceContext"][rules] represents an operator form of ResourceFunction["ReplaceContext"] that can be applied to an expression. | 
Retrieve the ResourceFunction:
| In[1]:= | ![ResourceFunction["ReplaceContext"]](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/020cae9447221cbf.png) | 
| Out[1]= |  | 
Replace symbols in the current context with symbols in a new context:
| In[2]:= | ![ResourceFunction[
 "ReplaceContext"][{a, b, c}, $Context -> "MyContext`"]](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/2257e787ffe205f2.png) | 
| Out[2]= |  | 
Use a list of replacement rules:
| In[3]:= | ![ResourceFunction[
 "ReplaceContext"][{a, b, c}, {$Context -> "MyContext`", "System`" -> "MySystem`"}]](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/440a9252888f45b3.png) | 
| Out[3]= |  | 
Use the operator form of ReplaceContext:
| In[4]:= | ![replace = ResourceFunction[
  "ReplaceContext"][{$Context -> "MyContext`", "System`" -> "MySystem`"}]](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/2c6553b4f511c34d.png) | 
| Out[4]= |  | 
| In[5]:= | ![replace[{f[x + y], g[z]}]](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/3d5ad84c7dba1665.png) | 
| Out[5]= |  | 
Copy a definition to a different context:
| In[6]:= | ![f[0] = 1;
f[x_Integer?Positive] := f[x] = f[x - 1]*x;](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/3d590dc95c93ac19.png) | 
| In[7]:= | ![Language`ExtendedFullDefinition[] = ResourceFunction["ReplaceContext"][
  Language`ExtendedDefinition[f], $Context -> "MySandbox`"]](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/3f37321ef9529b55.png) | 
| Out[7]= |  | 
Now code can be run in the new context without modifying definitions in the current context:
| In[8]:= | ![MySandbox`f /@ Range[5]](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/18dfb866d379ec83.png) | 
| Out[8]= |  | 
| In[9]:= | ![Definition[f]](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/446b3dd42f4a059e.png) | 
| Out[9]= |  | 
| In[10]:= | ![Definition[MySandbox`f]](https://www.wolframcloud.com/obj/resourcesystem/images/614/614ea1fe-9439-48f4-af30-d223d34c38bc/2f57101caeb0df5a.png) | 
| Out[10]= |  | 
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License