Function Repository Resource:

LookupKeys

Source Notebook

Look up a set of keys applying a function when a key is missing

Contributed by: Taliesin Beynon

ResourceFunction["LookupKeys"][assoc,keys,func]

looks up a set of keys, calling func[key] if a key is missing from assoc.

Examples

Basic Examples (2) 

Ask for a key:

In[1]:=
ResourceFunction["LookupKeys"][
 Association[{a -> x, b -> y, c -> z}], {a}]
Out[1]=

If a key is missing:

In[2]:=
ResourceFunction["LookupKeys"][
 Association[{a -> x, b -> y, c -> z}], {b, d}, missing]
Out[2]=

Applications (1) 

Get some data when a key is missing:

In[3]:=
ResourceFunction["LookupKeys"][
 PlanetData[PlanetData[], "Radius", "EntityAssociation"], {Entity["Planet", "Earth"], Entity["MinorPlanet", "Pluto"]}, MinorPlanetData[#, "Radius"] &]
Out[3]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 29 January 2019

License Information