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]=

Use a resource function as a symbol:

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

Scope (5) 

Specify the context:

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

Use a resource function from the new context:

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

Define a single resource function:

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

Use it:

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

Define a specific set of resource functions:

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

Try them out:

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

List symbols created by CreateResourceFunctionSymbols:

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

Other symbols are not included in this list:

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

Compare to Names:

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

Remove symbols created by CreateResourceFunctionSymbols:

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

Other symbols are not removed:

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

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]=
In[17]:=
res["Exists"]
Out[17]=

The definition is unchanged:

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

Overwrite any existing symbols:

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

ExcludedContexts (2) 

By default, some contexts are protected:

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

Override this protection (at your own risk):

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

AllowUnknownNames (2) 

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

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

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

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

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

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

Applications (2) 

Define all resource functions from a particular category:

In[28]:=
ResourceFunction["CreateResourceFunctionSymbols"]["WFP`", ResourceFunction["CategoryResourceFunctions"][
  "Wolfram Physics Project", "Name"]]
Out[28]=
In[29]:=
WFP`WolframModel[{{x, y}, {x, z}} -> {{x, z}, {x, w}, {y, w}, {z, w}}, {{0, 0}, {0, 0}}, 5, "StatesPlotsList"]
Out[29]=
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]=

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]=
In[33]:=
Stereogram3D[ResourceData["Stanford Bunny", "MeshRegion"], Boxed -> True]
Out[33]=
In[34]:=
StringFunction[Subsets]["string"]
Out[34]=

Properties and Relations (3) 

Get the list of created symbols:

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

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

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

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

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

Create a resource function symbol:

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

Check the definition:

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

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

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

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]
Out[48]=
In[49]:=
res["Created"]
Out[49]=
In[50]:=
res["Failed"]
Out[50]=

Version History

  • 1.0.0 – 25 January 2022

Related Resources

License Information