Function Repository Resource:

ValueMapIndexed

Source Notebook

Create an association from a set of values, using a map-indexed function on those values to form the keys

Contributed by: Seth J. Chandler

ResourceFunction["ValueMapIndexed"][func,list]

creates an association in which the values are list and keys are formed by map-indexing func over list.

ResourceFunction["ValueMapIndexed"][func]

is an operator form of ResourceFunction["ValueMapIndexed"] that can be applied to a list.

Examples

Basic Examples (2) 

ValueMapIndexed with a symbolic function f applied to a list of strings:

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

Creates an association where each key is a hash of a value from a given list and the position of that value in the list:

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

Scope (2) 

ValueMapIndexed works on the first level of expressions:

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

It can be made to work on lower levels by using Map in conjunction with ValueMapIndexed:

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

ValueMapIndexed works with Query on datasets:

In[5]:=
Query[ResourceFunction[
   "ValueMapIndexed"][#1 - ConstantArray[#2, Length[#1]] &]][
 Dataset[{{3, 4}, {5, 6}}]]
Out[5]=

It also works with implicit Query operations:

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

Applications (1) 

Create integer row headers for the Paul Revere social network:

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

Properties and Relations (1) 

The resource function ValueMap is a special case of ValueMapIndexed, in which one ignores the index argument:

In[8]:=
ResourceFunction["ValueMapIndexed"][f[#1] &, {a, b, c, d}] === ResourceFunction[
ResourceObject[<|{"Name" -> "ValueMap", "ShortName" -> "ValueMap", "UUID" -> "ec811401-31d3-48af-94ad-2b37839f3c46", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Similar to AssociationMap but maps over a given set of values to obtain keys rather than vice versa", "RepositoryLocation" -> URL[
       "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"],
       "SymbolName" -> "FunctionRepository`$b19ca6fbb4ba4d70be8e42e3476a7758`ValueMap", "FunctionLocation" -> CloudObject[
       "https://www.wolframcloud.com/obj/4c88f3e2-ebf5-4ce2-a9c6-89b6da02bb2f"]}|>, ResourceSystemBase -> "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"]][f[#1] &, {a, b, c, d}]
Out[8]=

Possible Issues (2) 

If a 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["ValueMapIndexed"][Length[#2] &, {"a", "b"}]
Out[9]=

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

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

Neat Examples (1) 

Use ValueMapIndexed 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[11]:=
Query[ResourceFunction["ValueMapIndexed"][
   Hash[{##}, "CRC32", "HexString"] &]][
 ExampleData[{"Dataset", "Titanic"}]]
Out[11]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 05 July 2019

Related Resources

License Information