Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Bind a symbol to a LocalSymbol so its value will automatically persist
ResourceFunction["BindToLocalSymbol"][sym] binds sym to a LocalSymbol. | |
ResourceFunction["BindToLocalSymbol"][sym,localsymbol] binds sym to localsymbol. | |
ResourceFunction["BindToLocalSymbol"][sym,"name"] binds sym to LocalSymbol["name"]. | |
ResourceFunction["BindToLocalSymbol"][sym,obj] binds sym to LocalSymbol[obj]. |
Bind a symbol to a LocalSymbol:
| In[1]:= |
| Out[1]= |
It initially has no value:
| In[2]:= |
| Out[2]= |
Assign it a value and the LocalSymbol will also update:
| In[3]:= |
| Out[4]= |
The reverse also works:
| In[5]:= |
| Out[6]= |
The value of the local symbol will persist between sessions and it can be bound to the symbol again:
| In[7]:= |
| In[8]:= |
| Out[8]= |
| In[9]:= |
| Out[10]= |
Bind to a specific LocalSymbol:
| In[11]:= |
| Out[11]= |
| In[12]:= |
| Out[13]= |
Bind to a local symbol with a specific name:
| In[14]:= |
| Out[14]= |
| In[15]:= |
| Out[16]= |
Other forms of LocalSymbol can also be used, like a file:
| In[17]:= | ![]() |
| Out[19]= |
Specify a directory:
| In[20]:= | ![]() |
| Out[23]= |
Use a path specified with File:
| In[24]:= | ![]() |
| Out[26]= |
Bound symbols can be assigned delayed values:
| In[27]:= |
| In[28]:= |
| Out[28]= |
Bound symbols and their local objects are removed with Remove:
| In[29]:= |
| Out[30]= |
Bound symbols work with reassignment functions such as Increment:
| In[31]:= |
| Out[31]= |
| In[32]:= |
| Out[33]= |
| In[34]:= |
| Out[35]= |
| In[36]:= |
| Out[37]= |
Bound symbols also work with functions like AppendTo:
| In[38]:= |
| Out[38]= |
| In[39]:= |
| Out[39]= |
| In[40]:= |
| Out[40]= |
As with LocalSymbol, setting a list of symbols at the same time will not work with a bound symbol:
| In[41]:= | ![]() |
| Out[42]= |
If a symbol already has a value when using BindToLocalSymbol, it will keep that value and override the stored value in the local symbol:
| In[43]:= | ![]() |
| Out[44]= |
If maintaining the value of the local symbol is desired, clear the value of the symbol first:
| In[45]:= | ![]() |
| Out[46]= |
When using Unset to separate the symbol and local symbol, the symbol will be assigned the current value of the local symbol using Set rather than SetDelayed:
| In[47]:= | ![]() |
| Out[48]= |
| In[49]:= |
| Out[50]= |
| In[51]:= |
| Out[52]= |
This work is licensed under a Creative Commons Attribution 4.0 International License