Function Repository Resource:

ConstantAssociation

Source Notebook

Create an association whose values are all a constant

Contributed by: Seth J. Chandler

ResourceFunction["ConstantAssociation"][keys,value]

creates an Association whose keys are keys and whose values are all value.

ResourceFunction["ConstantAssociation"][value]

is an operator form of ResourceFunction["ConstantAssociation"].

Details and Options

ResourceFunction["ConstantAssociation"] is a more expressive form of AssociationThread in which the second argument is the constant value. It also has an operator form.
This function is identical to a function found in the "GeneralUtilities`" context, except that it adds an operator form.
Generally, keys should be a List.

Examples

Basic Examples (3) 

Create an association with two keys and whose values are all 0:

In[1]:=
ResourceFunction["ConstantAssociation"][{"a", "b"}, 0]
Out[1]=

Create an association whose values are all Missing[]:

In[2]:=
ResourceFunction["ConstantAssociation"][{"height", "weight", "BMI"}, Missing[]]
Out[2]=

The operator form of ConstantAssociation:

In[3]:=
zeroAssociation = ResourceFunction["ConstantAssociation"][0];
In[4]:=
Map[zeroAssociation][{{"a", "b"}, {"c", "d", "e"}}]
Out[4]=

Scope (1) 

The constant value can be any Wolfram Language expression:

In[5]:=
ResourceFunction[
 "ConstantAssociation"][{"generator", "discriminator"}, NetChain[{ElementwiseLayer[Ramp], SummationLayer[]}]]
Out[5]=

Applications (4) 

Use the resource function JoinRest with ConstantAssociation to create default values for an Association that is missing certain key-value pairs:

In[6]:=
ResourceFunction["JoinRest"][
  ResourceFunction["ConstantAssociation"][{"a", "b", "c"}, 0]][<|"a" -> 6|>]
Out[6]=

Use the resource function JoinMost with ConstantAssociation to override values for certain keys in an association:

In[7]:=
ResourceFunction["JoinMost"][
  ResourceFunction["ConstantAssociation"][{"a", "b"}, Missing["erroneous reading"]]][<|"a" -> 6, "b" -> 5, "c" -> 8|>]
Out[7]=

Store and use an operator that will assign Missing[] as the value to a list of keys:

In[8]:=
missingAssociation = ResourceFunction["ConstantAssociation"][Missing[]];
In[9]:=
Map[missingAssociation][{{"a", "b"}, {"c", "d", "e"}}]
Out[9]=

Use ConstantAssociation in conjunction with the resource function JoinRest to convert a ragged list of associations into a regular one:

In[10]:=
Query[All, ResourceFunction["JoinRest"][
   ResourceFunction["ConstantAssociation"][{"a", "b", "c"}, 0]]][{Association["a" -> 4], Association["b" -> 8], ResourceFunction["ConstantAssociation"][{"a", "b", "c"}, 9]}]
Out[10]=

Neat Examples (1) 

Tally the frequency of the characters "a" through "j" in a list that is missing many of those characters:

In[11]:=
(ResourceFunction["JoinRest"])[
  ResourceFunction["ConstantAssociation"][CharacterRange["a", "j"], 0]][Counts[{"b", "c", "c", "b", "c", "b", "a", "d", "a", "a", "b", "d"}]]
Out[11]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 26 July 2019

Related Resources

License Information