Function Repository Resource:

ResourceFunctionPattern (1.0.0) current version: 1.1.0 »

Source Notebook

Generate a pattern that matches a resource function

Contributed by: Bob Sandheinrich

ResourceFunction["ResourceFunctionPattern"][rf]

creates a pattern that will match instances of the ResourceFunction rf.

ResourceFunction["ResourceFunctionPattern"]["name"]

creates a pattern that will match ResourceFunction objects with the named "name".

Details

ResourceFunction["ResourceFunctionPattern"]["name"] creates a pattern will match any resource function with the same name.
ResourceFunction["ResourceFunctionPattern"][rf] matches based on the unique ID inside rf and will not match other resource functions.
Attempting to match objects with a ResourceFunction head is more likely to success using ResourceFunction["ResourceFunctionPattern"][rf].
ResourceFunction["ResourceFunctionPattern"][ResourceFunction["name"]] is a convenient way to ensure the more powerful matching.

Examples

Basic Examples (3) 

Test if a ResourceFunction matches a pattern based on the name:

In[1]:=
MatchQ[ResourceFunction[
ResourceObject[<|"Name" -> "BirdSay", "ShortName" -> "BirdSay", "UUID" -> "a24858c8-45c5-4020-a8c2-c23bd5d26f04", "ResourceType" -> "Function", "Version" -> "3.0.0", "Description" -> "Have a bird say an expression", "RepositoryLocation" -> URL[
     "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$55711938e45341c08ca516066b27f30c`BirdSay", "FunctionLocation" -> CloudObject[
     "https://www.wolframcloud.com/obj/9d5d3776-baed-4a5d-bf38-e857d53825e1"]|>, ResourceSystemBase -> Automatic]], ResourceFunction["ResourceFunctionPattern"]["BirdSay"]]
Out[1]=

See the pattern for a name:

In[2]:=
ResourceFunction["ResourceFunctionPattern"]["WoodFramed"]
Out[2]=

Test against a pattern created from the ResourceFunction:

In[3]:=
MatchQ[ResourceFunction["BirdSay"], ResourceFunction["ResourceFunctionPattern"][
ResourceFunction[
ResourceObject[<|"Name" -> "BirdSay", "ShortName" -> "BirdSay", "UUID" -> "a24858c8-45c5-4020-a8c2-c23bd5d26f04", "ResourceType" -> "Function", "Version" -> "3.0.0", "Description" -> "Have a bird say an expression", "RepositoryLocation" -> URL[
      "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$55711938e45341c08ca516066b27f30c`BirdSay", "FunctionLocation" -> CloudObject[
      "https://www.wolframcloud.com/obj/9d5d3776-baed-4a5d-bf38-e857d53825e1"]|>, ResourceSystemBase -> Automatic]]]]
Out[3]=

Other functions do not match:

In[4]:=
MatchQ[ResourceFunction["BirdChat"], ResourceFunction["ResourceFunctionPattern"][
ResourceFunction[
ResourceObject[<|"Name" -> "BirdSay", "ShortName" -> "BirdSay", "UUID" -> "a24858c8-45c5-4020-a8c2-c23bd5d26f04", "ResourceType" -> "Function", "Version" -> "3.0.0", "Description" -> "Have a bird say an expression", "RepositoryLocation" -> URL[
      "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$55711938e45341c08ca516066b27f30c`BirdSay", "FunctionLocation" -> CloudObject[
      "https://www.wolframcloud.com/obj/9d5d3776-baed-4a5d-bf38-e857d53825e1"]|>, ResourceSystemBase -> Automatic]]]]
Out[4]=

Scope (3) 

Filter values using Cases:

In[5]:=
Cases[ResourceFunction[
   "ResourceFunctionSearch"][{"Categories" -> "Just For Fun", "game"}], ResourceFunction["ResourceFunctionPattern"]["Play2048"]]
Out[5]=

Filter in values using Select with a MatchQ operator:

In[6]:=
Select[ResourceFunction["ResourceFunctionSearch"]["image"], MatchQ[ResourceFunction["ResourceFunctionPattern"][
   "ImageColorReplace"]]]
Out[6]=

Create an operator:

In[7]:=
operator = ResourceFunction["ThroughOperator"][{Sin, Cos, Tan}]
Out[7]=

Match it against a pattern based on the ResourceFunction:

In[8]:=
MatchQ[operator, ResourceFunction["ResourceFunctionPattern"][
   ResourceFunction["ThroughOperator"]][___]]
Out[8]=

Applications (2) 

Create a function that takes a specific ResourceFunction as an argument:

In[9]:=
createPetName[
  method : ResourceFunction["ResourceFunctionPattern"]["RandomPetName"]] := method[]
In[10]:=
createPetName[
  method : ResourceFunction["ResourceFunctionPattern"]["RandomText"]] := method[2]
In[11]:=
createPetName[
  method : ResourceFunction["ResourceFunctionPattern"]["RandomString"]] := method[20]

Use the function:

In[12]:=
createPetName[
ResourceFunction[
ResourceObject[<|"Name" -> "RandomText", "ShortName" -> "RandomText", "UUID" -> "b72849e7-6789-4080-81a9-0ad149694d6a", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Generate a random piece of text", "RepositoryLocation" -> URL[
     "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$9957c9d76f414d4f88803edf53960b1a`RandomText", "FunctionLocation" -> CloudObject[
     "https://www.wolframcloud.com/obj/c24734d0-469f-4a6b-9d08-58fd86e20bb4"]|>, ResourceSystemBase -> Automatic]]]
Out[12]=

Create an expression containing resource functions used a objects:

In[13]:=
mathoperations = {
  ResourceFunction["FlipFunction"][Divide],
  Plus,
  ResourceFunction["FlipFunction"][Power],
  Times
  }
Out[13]=

Replace the objects:

In[14]:=
mathoperations /. {ResourceFunction["ResourceFunctionPattern"][
     ResourceFunction["FlipFunction"]][f_, args___] :> OperatorApplied[f, args]}
Out[14]=

Properties and Relations (3) 

Define two resource functions with the same name:

In[15]:=
rf1 = DefineResourceFunction[5 + # &, "AddFive"]
Out[15]=
In[16]:=
rf2 = DefineResourceFunction[Append[#, 5] &, "AddFive"]
Out[16]=

They both match a pattern defined with the name:

In[17]:=
MatchQ[rf1, ResourceFunction["ResourceFunctionPattern"]["AddFive"]]
Out[17]=
In[18]:=
MatchQ[rf2, ResourceFunction["ResourceFunctionPattern"]["AddFive"]]
Out[18]=

They do not match a pattern based on the other ResourceFunction:

In[19]:=
MatchQ[rf2, ResourceFunction["ResourceFunctionPattern"][rf1]]
Out[19]=
In[20]:=
MatchQ[rf1, ResourceFunction["ResourceFunctionPattern"][rf2]]
Out[20]=

Possible Issues (3) 

Create an operator:

In[21]:=
operator = ResourceFunction["ThroughOperator"][{Sin, Cos, Tan}]
Out[21]=

The match fails based only on a name:

In[22]:=
MatchQ[operator, ResourceFunction["ResourceFunctionPattern"]["ThroughOperator"][___]]
Out[22]=

Match it against a pattern based on the ResourceFunction:

In[23]:=
MatchQ[operator, ResourceFunction["ResourceFunctionPattern"][
   ResourceFunction["ThroughOperator"]][___]]
Out[23]=

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.1.0 – 04 September 2024
  • 1.0.0 – 30 August 2024

Related Resources

License Information