Function Repository Resource:

KeyCases

Source Notebook

Choose elements of an association by matching keys to a pattern

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["KeyCases"][assoc,patt]

selects elements in the association assoc for which their keys match patt.

ResourceFunction["KeyCases"][patt]

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

Details

ResourceFunction["KeyCases"] yields an Association object whose elements appear in the same order as they did in assoc.
ResourceFunction["KeyCases"] can be applied not only to Association objects, but also to any expression that has rules for arguments.
ResourceFunction["KeyCases"][patt][assoc] is equivalent to ResourceFunction["KeyCases"][assoc,patt].

Examples

Basic Examples (2) 

Choose keys in an association that match a pattern:

In[1]:=
ResourceFunction["KeyCases"][<|"a" -> 1, "b" -> 2, "c" -> 3|>, "a" | "b"]
Out[1]=
In[2]:=
ResourceFunction["KeyCases"][<|"a" -> 1, b -> 2, "c" -> 3|>, _String]
Out[2]=
In[3]:=
ResourceFunction["KeyCases"][<|1 -> 1, 2 :> 2^2, 3 :> 3^2|>, n_ /; n < 3]
Out[3]=

Use the operator form of KeyCases:

In[4]:=
ResourceFunction["KeyCases"]["a" | "b"][<|"a" -> 1, "b" -> 2, "c" -> 3|>]
Out[4]=

Scope (3) 

Use a list of rules:

In[5]:=
ResourceFunction["KeyCases"][{"a" -> 1, "b" -> 2, "c" -> 3}, "a" | "b"]
Out[5]=

KeyCases accepts any expression whose arguments consist of Rule or RuleDelayed key-value pairs:

In[6]:=
ResourceFunction["KeyCases"][MyData[a -> 1, b -> 2, c -> 3], a | b]
Out[6]=

Use Unevaluated and HoldPattern to prevent evaluation:

In[7]:=
ResourceFunction["KeyCases"][
 Unevaluated[{Echo["a"] -> 1 + 1, Echo["b"] -> 2 + 2, Echo["c"] -> 3 + 3}], HoldPattern[Echo["a" | "b"]]]
Out[7]=

Applications (1) 

Filter definitions for a function:

In[8]:=
fib[0] := 0;
fib[1] := 1;
fib[x_Integer?Positive] := fib[x] = fib[x - 1] + fib[x - 2];
fib[___] := $Failed;
In[9]:=
fib[500]
Out[9]=
In[10]:=
DownValues[fib]
Out[10]=
In[11]:=
ResourceFunction["KeyCases"][%, HoldPattern[_[fib[Except[_Integer]]]]]
Out[11]=

Properties and Relations (3) 

KeyCases[as,patt] is effectively equivalent to KeySelect[as,MatchQ[patt]]:

In[12]:=
ResourceFunction["KeyCases"][<|"a" -> 1, "b" -> 2, "c" -> 3|>, "a" | "b"]
Out[12]=
In[13]:=
KeySelect[<|"a" -> 1, "b" -> 2, "c" -> 3|>, MatchQ["a" | "b"]]
Out[13]=

KeyCases will not evaluate keys when matching:

In[14]:=
as = Block[{Echo}, <|Echo["key"] -> Echo["value"]|>]
Out[14]=
In[15]:=
ResourceFunction["KeyCases"][as, _Echo]
Out[15]=

Compare to KeySelect with MatchQ:

In[16]:=
KeySelect[as, MatchQ[_Echo]]
Out[16]=

Possible Issues (2) 

KeyCases returns an Association, so duplicates are not preserved:

In[17]:=
ResourceFunction["KeyCases"][{"a" -> 1, "b" -> 2, "c" -> 3, "b" -> 4},
  "a" | "b"]
Out[17]=

This behavior is consistent with KeySelect:

In[18]:=
KeySelect[{"a" -> 1, "b" -> 2, "c" -> 3, "b" -> 4}, MatchQ["a" | "b"]]
Out[18]=

All arguments must be valid rules:

In[19]:=
ResourceFunction["KeyCases"][MyData[x, a -> 1, b -> 2, c -> 3], a | b]
Out[19]=

Neat Examples (1) 

Reproduce the behavior of UnevaluatedAssociation:

In[20]:=
ResourceFunction["KeyCases"][
 Unevaluated[<|1 + 1 -> Echo[2], 2 + 2 -> Echo[4]|>], _]
Out[20]=
In[21]:=
ResourceFunction["UnevaluatedAssociation"][1 + 1 -> Echo[2], 2 + 2 -> Echo[4]]
Out[21]=

Version History

  • 1.0.0 – 06 January 2023

Related Resources

License Information