Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Choose elements of an association by matching keys to a pattern
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. |
Choose keys in an association that match a pattern:
In[1]:= |
Out[1]= |
In[2]:= |
Out[2]= |
In[3]:= |
Out[3]= |
Use the operator form of KeyCases:
In[4]:= |
Out[4]= |
Use a list of rules:
In[5]:= |
Out[5]= |
KeyCases accepts any expression whose arguments consist of Rule or RuleDelayed key-value pairs:
In[6]:= |
Out[6]= |
Use Unevaluated and HoldPattern to prevent evaluation:
In[7]:= |
Out[7]= |
Filter definitions for a function:
In[8]:= |
In[9]:= |
Out[9]= |
In[10]:= |
Out[10]= |
In[11]:= |
Out[11]= |
KeyCases[as,patt] is effectively equivalent to KeySelect[as,MatchQ[patt]]:
In[12]:= |
Out[12]= |
In[13]:= |
Out[13]= |
KeyCases will not evaluate keys when matching:
In[14]:= |
Out[14]= |
In[15]:= |
Out[15]= |
Compare to KeySelect with MatchQ:
In[16]:= |
Out[16]= |
KeyCases returns an Association, so duplicates are not preserved:
In[17]:= |
Out[17]= |
This behavior is consistent with KeySelect:
In[18]:= |
Out[18]= |
All arguments must be valid rules:
In[19]:= |
Out[19]= |
This work is licensed under a Creative Commons Attribution 4.0 International License