Wolfram Research

Function Repository Resource:

AssociationThrough

Source Notebook

Generate an association from applying different operations to an expression

Contributed by: Andrew Steinacher

ResourceFunction["AssociationThrough"][{f1,f2,},x]

gives <|f1f1[x],f2f2[x],|>.

ResourceFunction["AssociationThrough"][{f1label1,f2label2,},x]

gives <|label1f1[x],label2f2[x],|>.

ResourceFunction["AssociationThrough"][f]

represents an operator form of ResourceFunction["AssociationThrough"] that can be applied to an expression.

Details and Options

ResourceFunction["AssociationThrough"][{f1,f2,},x] is equivalent to AssociationMap[#[x]&,{f1,f2}].

Examples

Basic Examples (2) 

Generate an Association to label the results of different operations on an expression:

In[1]:=
ResourceFunction["AssociationThrough"][{f, g}, {1, 2, 3, 4}]
Out[1]=

Give the operations labels:

In[2]:=
ResourceFunction[
 "AssociationThrough"][{f -> "F", g -> "G"}, {1, 2, 3, 4}]
Out[2]=

Perform multiple statistical operations on a single distribution, returning a single expression:

In[3]:=
ResourceFunction[
 "AssociationThrough"][{Min, Max, StandardDeviation}, {1, 2, 3, 4}]
Out[3]=

Scope (2) 

Adding bulletproofing to statistical operations complicates the keys of the resulting expression:

In[4]:=
ResourceFunction[
 "AssociationThrough"][{Mean, Replace[{x_} :> {x, x}] /* StandardDeviation}, {1}]
Out[4]=

Use a more appropriate label to hide the complications in the result:

In[5]:=
ResourceFunction[
 "AssociationThrough"][{Mean, Replace[{_} -> {0, 0}] /* StandardDeviation -> StandardDeviation}, {1}]
Out[5]=

Applications (3) 

Use an operator form to be applied to many expressions:

In[6]:=
stats = ResourceFunction[
   "AssociationThrough"][{Length -> "Count", Min, MeanAround, Max}];
In[7]:=
stats /@ RandomInteger[10, {3, 5}]
Out[7]=

Use formatted names as labels and show in a grid:

In[8]:=
ResourceFunction["NiceGrid"][
 Map[
  ResourceFunction["AssociationThrough"][
   Style[#, Bold, 18] & /@ <|Length -> "Count", Min -> "Lowest", Mean -> "Average", Max -> "Highest"|>],
  <|"A" -> RandomReal[1, 10], "B" -> RandomReal[2, 10], "C" -> RandomReal[3, 10]|>
  ]
 ]
Out[8]=

Write a function to generate a simple association of statistical information that is robust to unclean data:

In[9]:=
robustStats = DeleteMissing /* ResourceFunction["AssociationThrough"][
    {
     Length,
     Replace[{{} -> None, l_List :> Median[l]}] -> Median,
     Replace[{{_} -> 0, l : {__} :> StandardDeviation[l], _ -> None}] -> StandardDeviation
     }
    ];
In[10]:=
data = robustStats /@ {{}, {1}, {1, 2, 3}, {1, Missing["NotAvailable"]}}
Out[10]=

Values can be extracted using simple keys:

In[11]:=
Lookup[data, {StandardDeviation, Median}]
Out[11]=

Properties and Relations (3) 

AssociationThrough is similar to a special case of AssociationMap:

In[12]:=
AssociationMap[#[{a, b, c, d}] &, {f, g, h}]
Out[12]=

AssociationThrough can give different labels for the keys:

In[13]:=
ResourceFunction[
 "AssociationThrough"][{f -> "F", g -> "G"}, {a, b, c, d}]
Out[13]=

A similar operation can be done with a combination of AssociationThread and Through:

In[14]:=
AssociationThread[{"F", "G"}, Through[{f, g}[{a, b, c, d}]]]
Out[14]=

Possible Issues (2) 

If there are duplicate operations, all but the last is dropped:

In[15]:=
ResourceFunction["AssociationThrough"][{f, g, f}, {a, b, c, d}]
Out[15]=

If there are duplicate labels, all but the last is dropped:

In[16]:=
ResourceFunction["AssociationThrough"][{f -> g, g, f}, {a, b, c, d}]
Out[16]=

Resource History

Related Resources

Author Notes

Although combinations of AssociationThread, Through, AssociationMap and similar can achieve this same effect, it's difficult to remember how to use them in this fashion. It's even more difficult to handle complicated operations while generating "nice" labels in a clean, concise and robust way… these two problems occur frequently enough for this function to be useful.

License Information