Function Repository Resource:

ValueMap

Source Notebook

Similar to AssociationMap but maps over a given set of values to obtain keys rather than vice versa

Contributed by: Seth J. Chandler

ResourceFunction["ValueMap"][func,x]

creates an Association in which the keys are the mapping of func over x and the values are x.

ResourceFunction["ValueMap"][func]

an operator form that, when presented with a list of values x, will create ResourceFunction["ValueMap"][func,x].

Examples

Basic Examples (2) 

Create keys by mapping symbolic function f over a list of strings:

In[1]:=
ResourceFunction["ValueMap"][f, {"a", "ab", "abc"}]
Out[1]=

Create an Association between hashes of the values and the values themselves:

In[2]:=
ResourceFunction["ValueMap"][
 Hash[#, "CRC32", "HexString"] &, {"a", "ab", "abc"}]
Out[2]=

Scope (2) 

ValueMap works on the first level of expressions:

In[3]:=
ResourceFunction["ValueMap"][f, IdentityMatrix[3]]
Out[3]=

Work on lower levels by using Map in conjunction with ValueMap:

In[4]:=
Map[ResourceFunction["ValueMap"][f], IdentityMatrix[3]]
Out[4]=

ValueMap works with Query on datasets:

In[5]:=
Query[ResourceFunction["ValueMap"][Total]][Dataset[{{3, 4}, {5, 6}}]]
Out[5]=

It also works with implicit Query operations:

In[6]:=
Dataset[{{3, 4}, {5, 6}}][ResourceFunction["ValueMap"][Total]]
Out[6]=

Applications (1) 

Create row headers for the Paul Revere social network based on the name of the person involved:

In[7]:=
Query[ResourceFunction["ValueMap"][#Name &]][
 ResourceData["Paul Revere's Social Network in Colonial Boston"]]
Out[7]=

Properties and Relations (1) 

One can accomplish much the same thing as ValueMap with the following code that uses AssociationMap; however, it is quite cumbersome:

In[8]:=
Association[
  KeyValueMap[Rule @@ Reverse[{#1, #2}] &, AssociationMap[f, {a, b, c, d}]]] === ResourceFunction["ValueMap"][f, {a, b, c, d}]
Out[8]=

Possible Issues (2) 

If the key generated from application of the function is not unique, the value for that key will be the last value encountered that generated the key:

In[9]:=
ResourceFunction["ValueMap"][StringLength, {"a", "b"}]
Out[9]=

Sometimes, this deletion of key-value pairs with duplicate keys may be exactly what is desired, however:

In[10]:=
ResourceFunction["ValueMap"][
  Hash[#, "CRC32", "HexString"] &][{Association["first" -> "John", "last" -> "Smith"], Association["first" -> "Mary", "last" -> "Jones"], Association["first" -> "John", "last" -> "Smith"] (* a mistaken duplicate*)}]
Out[10]=

ValueMap will not work properly on datasets without the use of Query:

In[11]:=
ResourceFunction["ValueMap"][f][Dataset[{{2, 3}, {4, 5}}]]
Out[11]=

Neat Examples (1) 

Use ValueMap to create a custom index on the hash of a function of two columns of a dataset and then use the index to swiftly retrieve information:

In[12]:=
Module[{crcHash = Hash[#, "CRC32", "HexString"] &, extractor = {ToExpression@DateValue[#[[1]], "Year"], #[[3]]} &, indexedData},
 indexedData = Query[ResourceFunction["ValueMap"][(extractor/*crcHash)]][
   Normal@ResourceData["Supreme Court Justice Database"]];
 Lookup[indexedData, crcHash[{1943, "Jackson"}]]
 ]
Out[12]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 26 June 2019

Related Resources

License Information