Function Repository Resource:

CreateResourceFunctionSymbols

Source Notebook

Create a symbol for each named resource function

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["CreateResourceFunctionSymbols"][]

defines a symbol for each named ResourceFunction in the "RF`" context.

ResourceFunction["CreateResourceFunctionSymbols"]["ctx`"]

defines symbols in the given context.

ResourceFunction["CreateResourceFunctionSymbols"]["ctx`",{"name1","name2",}]

only defines symbols for the given names.

ResourceFunction["CreateResourceFunctionSymbols"]["ctx`",names,"op"]

performs the operation specified by "op" on names.

Details and Options

ResourceFunction["CreateResourceFunctionSymbols"][] is equivalent to ResourceFunction["CreateResourceFunctionSymbols"][Automatic].
ResourceFunction["CreateResourceFunctionSymbols"][context] is equivalent to ResourceFunction["CreateResourceFunctionSymbols"][context,All].
ResourceFunction["CreateResourceFunctionSymbols"] accepts the following options:
OverwriteTargetFalsewhether to redefine the target symbol if it already exists
ExcludedContextsAutomatica list of contexts that should be protected from changes
ResourceSystemBaseAutomaticthe resource system to obtain names from
AllowUnknownNamesTruewhether to set definitions for unknown resource names
When OverwriteTarget is set to False, a target symbol will not be redefined if it has any existing definitions.
ExcludedContexts is meant to be a safety mechanism to prevent changing definitions in important system contexts.
ExcludedContextsNone can be used to force ResourceFunction["CreateResourceFunctionSymbols"] to define symbols in a system context, but this is not recommended.
When AllowUnknownNames is set to True, ResourceFunction["CreateResourceFunctionSymbols"]["ctx`",names] will define symbols for all names, even if they do not correspond to a known resource function name.
The operation specified by "op" can be one of the following:
"List"list the symbols created
"Remove"remove the symbols created
"Create"equivalent to the two-argument form

Examples

Basic Examples (2) 

Create symbols for each resource function:

In[1]:=
ResourceFunction["CreateResourceFunctionSymbols"][]
Out[1]=
Image

Use a resource function as a symbol:

In[2]:=
RF`BirdSay["neat"]
Out[2]=
Image

Scope (5) 

Specify the context:

In[3]:=
ResourceFunction["CreateResourceFunctionSymbols"]["MyContext`"]
Out[3]=
Image

Use a resource function from the new context:

In[4]:=
MyContext`PartyParrot["Birdnardo"]
Out[4]=
Image

Define a single resource function:

In[5]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["BirdStuff`", "BirdSay"]
Out[5]=
Image

Use it:

In[6]:=
BirdStuff`BirdSay["neat"]
Out[6]=
Image

Define a specific set of resource functions:

In[7]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["NewContext`", {"PartyParrot", "SpeechBubble"}]
Out[7]=
Image

Try them out:

In[8]:=
NewContext`SpeechBubble[
 NewContext`PartyParrot["Birdnardo"], "I'm the new BirdSay", Scaled[.25]]
Out[8]=
Image

List symbols created by CreateResourceFunctionSymbols:

In[9]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["NewContext`", All, "List"]
Out[9]=
Image

Other symbols are not included in this list:

In[10]:=
NewContext`MyFunction[x_] := x + 1;
In[11]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["NewContext`", All, "List"]
Out[11]=
Image

Compare to Names:

In[12]:=
Names["NewContext`*"]
Out[12]=
Image

Remove symbols created by CreateResourceFunctionSymbols:

In[13]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["NewContext`", All, "Remove"]
Out[13]=
Image

Other symbols are not removed:

In[14]:=
Names["NewContext`*"]
Out[14]=
Image

Options (7) 

OverwriteTarget (3) 

By default, existing symbols with definitions won't be redefined:

In[15]:=
EmptyContext`BirdSay[expr_] := ResourceFunction["SpeechBubble"][Show[\!\(\*
NamespaceBox["LinguisticAssistant",
DynamicModuleBox[{Typeset`query$$ = "picture of a bird", Typeset`boxes$$ = RowBox[{
TemplateBox[{"\"birds\"", 
RowBox[{"Entity", "[", 
RowBox[{"\"Species\"", ",", "\"Class:Aves\""}], "]"}], "\"Entity[\\\"Species\\\", \\\"Class:Aves\\\"]\"", "\"species specification\""}, "Entity"], "[", 
TemplateBox[{"\"image\"", 
RowBox[{"EntityProperty", "[", 
RowBox[{"\"Species\"", ",", "\"Image\""}], "]"}], "\"EntityProperty[\\\"Species\\\", \\\"Image\\\"]\""}, "EntityProperty"], "]"}], Typeset`allassumptions$$ = {{"type" -> "MultiClash", "word" -> "", "template" -> "Assuming ${word1} is referring to ${desc1}. Use \"${word2}\" as ${desc2}. Use \"${word3}\" as ${desc3}.", "count" -> "3", "Values" -> {{"name" -> "Species", "word" -> "bird", "desc" -> "a species specification", "input" -> "*MC.%7E-_*Species-"}, {"name" -> "Person", "word" -> "a bird", "desc" -> "a person", "input" -> "*MC.%7E-_*Person-"}, {"name" -> "WordData", "word" -> "", "desc" -> " referring to English words", "input" -> "*MC.%7E-_*WordData-"}}}}, Typeset`assumptions$$ = {}, Typeset`open$$ = {1}, Typeset`querystate$$ = {"Online" -> True, "Allowed" -> True, "mparse.jsp" -> 3.4309153, "Messages" -> {}}}, 
DynamicBox[ToBoxes[
AlphaIntegration`LinguisticAssistantBoxes["", 4, Automatic, 
Dynamic[Typeset`query$$], 
Dynamic[Typeset`boxes$$], 
Dynamic[Typeset`allassumptions$$], 
Dynamic[Typeset`assumptions$$], 
Dynamic[Typeset`open$$], 
Dynamic[Typeset`querystate$$]], StandardForm],
ImageSizeCache->{124.8, {9., 15.8}},
TrackedSymbols:>{Typeset`query$$, Typeset`boxes$$, Typeset`allassumptions$$, Typeset`assumptions$$, Typeset`open$$, Typeset`querystate$$}],
DynamicModuleValues:>{},
UndoTrackedVariables:>{Typeset`open$$}],
BaseStyle->{"Deploy"},
DeleteWithContents->True,
Editable->False,
SelectWithContents->True]\), ImageSize -> 75], expr, Scaled[.25]];
In[16]:=
res = ResourceFunction["CreateResourceFunctionSymbols"][
  "EmptyContext`", {"BirdSay", "PartyParrot", "SpeechBubble"}]
Out[16]=
Image
In[17]:=
res["Exists"]
Out[17]=
Image

The definition is unchanged:

In[18]:=
EmptyContext`BirdSay["neat"]
Out[18]=
Image

Overwrite any existing symbols:

In[19]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["EmptyContext`", {"BirdSay", "PartyParrot", "SpeechBubble"}, OverwriteTarget -> True]
Out[19]=
Image
In[20]:=
EmptyContext`BirdSay["neat"]
Out[20]=
Image

ExcludedContexts (2) 

By default, some contexts are protected:

In[21]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["System`", "ContainsQ"]
Image
Out[21]=
Image

Override this protection (at your own risk):

In[22]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["System`", "ContainsQ", ExcludedContexts -> None]
Out[22]=
Image
In[23]:=
ContainsQ[x + y^z, Power[_, _]]
Out[23]=
Image
In[24]:=
ContainsQ[x + y^z, Times[_, _]]
Out[24]=
Image

AllowUnknownNames (2) 

By default, CreateResourceFunctionSymbol will allow defining symbols for names that are not recognized:

In[25]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["MyContext`", "DoesNotExist"]
Out[25]=
Image

These symbols will fail at runtime if used before the corresponding ResourceFunction exists:

In[26]:=
MyContext`DoesNotExist
Image
Image
Out[26]=
Image

Use "AllowUnknownNames"->False to ensure that unrecognized names will cause a failure:

In[27]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["MyContext`", "AlsoDoesNotExist", "AllowUnknownNames" -> False]
Image
Out[27]=
Image

Applications (2) 

Define all resource functions from a particular category:

In[28]:=
ResourceFunction["CreateResourceFunctionSymbols"]["WFP`", ResourceFunction["CategoryResourceFunctions"][
  "Wolfram Physics Project", "Name"]]
Out[28]=
Image
In[29]:=
WFP`WolframModel[{{x, y}, {x, z}} -> {{x, z}, {x, w}, {y, w}, {z, w}}, {{0, 0}, {0, 0}}, 5, "StatesPlotsList"]
Out[29]=
Image
In[30]:=
WFP`GraphReconstructedSurface[
 WFP`WolframModel[{{x, y}, {x, z}} -> {{x, z}, {x, w}, {y, w}, {z, w}}, {{0, 0}, {0, 0}}, 10, "FinalState"]]
Out[30]=
Image

Create symbols in the current context to reference resource functions by their base name:

In[31]:=
ResourceFunction["CreateResourceFunctionSymbols"][$Context];

Use the resource functions FoldRight, Stereogram3D and StringFunction:

In[32]:=
FoldRight[f, x, {a, b, c, d}]
Out[32]=
Image
In[33]:=
Stereogram3D[ResourceData["Stanford Bunny", "MeshRegion"], Boxed -> True]
Out[33]=
Image
In[34]:=
StringFunction[Subsets]["string"]
Out[34]=
Image

Properties and Relations (3) 

Get the list of created symbols:

In[35]:=
res = ResourceFunction["CreateResourceFunctionSymbols"][
  "RedefineTest`", {"BirdSay", "PartyParrot"}]
Out[35]=
Image
In[36]:=
res["Created"]
Out[36]=
Image

Symbols that have already been created by CreateResourceFunctionSymbols are returned as Missing objects:

In[37]:=
res = ResourceFunction["CreateResourceFunctionSymbols"][
  "RedefineTest`", {"BirdSay", "PartyParrot", "SpeechBubble"}]
Out[37]=
Image
In[38]:=
res["Exists"]
Out[38]=
Image
In[39]:=
res["Created"]
Out[39]=
Image

User-created resource functions will also be included if they are discoverable by name:

In[40]:=
DefineResourceFunction[# + 1 &, "AddOne"]
Out[40]=
Image
In[41]:=
ResourceFunction["CreateResourceFunctionSymbols"][];
In[42]:=
RF`AddOne[5]
Out[42]=
Image

Create a resource function symbol:

In[43]:=
ResourceFunction[
 "CreateResourceFunctionSymbols"]["DefinitionExample`", "GrayCode"]
Out[43]=
Image

Check the definition:

In[44]:=
Definition[DefinitionExample`GrayCode]
Out[44]=
Image

The definition changes after the symbol is used for the first time:

In[45]:=
DefinitionExample`GrayCode[14]
Out[45]=
Image
In[46]:=
Definition[DefinitionExample`GrayCode] // InputForm // ToString
Out[46]=
Image

Possible Issues (1) 

Locked symbols cannot be overwritten:

In[47]:=
AnotherContext`BirdSay = "nope";
SetAttributes[AnotherContext`BirdSay, Locked];
In[48]:=
res = ResourceFunction["CreateResourceFunctionSymbols"][
  "AnotherContext`", {"BirdSay", "PartyParrot", "SpeechBubble"}, OverwriteTarget -> True]
Image
Out[48]=
Image
In[49]:=
res["Created"]
Out[49]=
Image
In[50]:=
res["Failed"]
Out[50]=
Image

Version History

  • 1.0.0 – 25 January 2022

Related Resources

License Information