Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Define a data structure that can refer to its own values to compute new ones
ResourceFunction["AssociationTemplate"][assoc] creates an Association-like object in which RuleDelayed (⧴) values can be combined with TemplateSlots and Functions to create values that depend on other keys. |
Define an AssociationTemplate in which the first key acts as a parameter for a computation:
In[1]:= | ![]() |
Out[1]= | ![]() |
Query a value:
In[2]:= | ![]() |
Out[2]= | ![]() |
Use a two-argument query to temporarily override keys with new (non-templated) values. The second argument has to be an Association:
In[3]:= | ![]() |
Out[3]= | ![]() |
Query all keys and return the result as an Association:
In[4]:= | ![]() |
Out[4]= | ![]() |
Use Append or Prepend to create a new AssociationTemplate with a different key value:
In[5]:= | ![]() |
Out[5]= | ![]() |
In[6]:= | ![]() |
Out[6]= | ![]() |
AssociationTemplate can be used with the functions Length, Keys, Values and Normal:
In[7]:= | ![]() |
In[8]:= | ![]() |
Out[8]= | ![]() |
In[9]:= | ![]() |
Out[9]= | ![]() |
In[10]:= | ![]() |
Out[10]= | ![]() |
Normal will turn all templated values into a TemplateExpression or TemplateObject or leave them as a Function:
In[11]:= | ![]() |
Out[11]= | ![]() |
Join two templates together:
In[12]:= | ![]() |
Out[12]= | ![]() |
In[13]:= | ![]() |
Out[13]= | ![]() |
It is possible that some keys can return different values with each query:
In[14]:= | ![]() |
In[15]:= | ![]() |
Out[15]= | ![]() |
Within a single query, the values will be consistent and each key will be evaluated no more than once:
In[16]:= | ![]() |
Out[16]= | ![]() |
In[17]:= | ![]() |
Out[17]= | ![]() |
In[18]:= | ![]() |
Out[18]= | ![]() |
AssociationTemplate will return a Failure object if cyclical dependencies are detected:
In[19]:= | ![]() |
Out[19]= | ![]() |
In[20]:= | ![]() |
Out[20]= | ![]() |
In[21]:= | ![]() |
Out[21]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License