Function Repository Resource:

AssociationReference

Source Notebook

Create an association with self references

Contributed by: Bob Sandheinrich

ResourceFunction["AssociationReference"][key]

represents a reference to another value in an Association.

ResourceFunction["AssociationReference"][<|k1val1,k2val2,|>]

represents an Association where instances of ResourceFunction["AssociationReference"][ki] within the valj are resolved to the values vali.

Details

ResourceFunction["AssociationReference"] can reduce the size of an Association by eliminating repeated large values.
Any vali expressions containing ResourceFunction["AssociationReference"] should be defined using RuleDelayed.

Examples

Basic Examples (2) 

Create an AssociationReference object with three keys, two of which reference the other:

In[1]:=
as = ResourceFunction[
  "AssociationReference"][<|"numbers" -> Range[100], "max" :> Max[ResourceFunction["AssociationReference"]["numbers"]], "min" :> Min[ResourceFunction["AssociationReference"]["numbers"]]|>]
Out[1]=
In[2]:=
as["max"]
Out[2]=
In[3]:=
as["min"]
Out[3]=
In[4]:=
as["numbers"]
Out[4]=

In[5]:=
as = ResourceFunction[
  "AssociationReference"][<|"numbers" -> Range[100], "max" :> Max[ResourceFunction["AssociationReference"]["numbers"]], "min" :> Min[ResourceFunction["AssociationReference"]["numbers"]], "range" :> (ResourceFunction["AssociationReference"]["max"] - ResourceFunction["AssociationReference"]["min"])|>]
Out[5]=
In[6]:=
as["range"]
Out[6]=
In[7]:=
as["min"]
Out[7]=
In[8]:=
as["numbers"]
Out[8]=

Scope (4) 

Many functions work on AssociationReference objects like on Association objects:

In[9]:=
as = ResourceFunction[
  "AssociationReference"][<|"numbers" -> Range[100],
   "max" :> Max[ResourceFunction["AssociationReference"]["numbers"]], "min" :> Min[ResourceFunction["AssociationReference"]["numbers"]]|>]
Out[9]=

Part gives values:

In[10]:=
as[["min"]]
Out[10]=

Key limiting operations return a new AssociationReference object:

In[11]:=
KeyTake[as, {"max", "numbers"}]
Out[11]=
In[12]:=
KeyDrop[as, {"max"}]
Out[12]=
In[13]:=
Keys[as]
Out[13]=
In[14]:=
Select[as, ListQ]
Out[14]=
In[15]:=
Select[as, OddQ]
Out[15]=
In[16]:=
DeleteDuplicatesBy[
 ResourceFunction[
  "AssociationReference"][<|"a" -> 1, "b" -> -1, "c" :> ResourceFunction["AssociationReference"]["a"], "d" -> -2|>], Abs]
Out[16]=

Values and Normal return the final value for each key:

In[17]:=
Values[as]
Out[17]=
In[18]:=
Normal[as]
Out[18]=
In[19]:=
KeyExistsQ[as, "max"]
Out[19]=

A few basic statistical operations are supported:

In[20]:=
as = ResourceFunction[
  "AssociationReference"][<|"a" -> 10, "b" :> 2* ResourceFunction["AssociationReference"]["a"], "c" :> ResourceFunction["AssociationReference"]["b"]/4|>]
Out[20]=
In[21]:=
Through[{Mean, Max, Min, Total, Variance, MinMax, Median}[as]]
Out[21]=
In[22]:=
Quantile[as, 3/4]
Out[22]=

Sorting operations return a new AssociationReference object:

In[23]:=
Sort[ResourceFunction[
  "AssociationReference"][<|"a" -> 3, "b" -> 2, "c" -> 1|>]]
Out[23]=
In[24]:=
KeySort[%]
Out[24]=
In[25]:=
Prepend[ResourceFunction[
  "AssociationReference"][{"cow" -> "moo", "chicken" -> "cluck"}], "horse" -> "neigh"]
Out[25]=

Use Map to map a function over values:

In[26]:=
as = f /@ ResourceFunction[
   "AssociationReference"][<|
    "Capitol" -> Entity["City", {"JeffersonCity", "Missouri", "UnitedStates"}], "LargestCity" -> Entity["City", {"SaintLouis", "Missouri", "UnitedStates"}], "Ratio" :> ResourceFunction["AssociationReference"]["LargestCity"]/
      ResourceFunction["AssociationReference"]["Capitol"]|>]
Out[26]=
In[27]:=
as // InputForm
Out[27]=

When the value is retrieved, the function is applied on each value:

In[28]:=
as["Ratio"]
Out[28]=

Possible Issues (2) 

If keys are removed, the resulting AssociationReference object can give values with Missing instead of the dependency:

In[29]:=
as = KeyTake[
  ResourceFunction[
   "AssociationReference"][<|"three" -> 3, "two" :> ResourceFunction["AssociationReference"]["three"] - 1|>],
   "two"]
Out[29]=
In[30]:=
as["two"]
Out[30]=

Loops are bad:

In[31]:=
ResourceFunction[
  "AssociationReference"][<|
   "key" :> ResourceFunction["AssociationReference"]["key"]|>]["key"]
Out[31]=

Version History

  • 1.0.0 – 08 July 2022

Related Resources

License Information