Function Repository Resource:

FormatAsResourceFunction

Source Notebook

Format a symbol as a ResourceFunction in outputs

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["FormatAsResourceFunction"][symbol]

formats symbol as a ResourceFunction of the same name.

ResourceFunction["FormatAsResourceFunction"][symbol,"name"]

formats symbol as a ResourceFunction with the name "name".

ResourceFunction["FormatAsResourceFunction"][symbol,"name",id]

formats using an Interpretation giving ResourceFunction[id].

Details and Options

Using ResourceFunction["FormatAsResourceFunction"] in the definition of other resource functions can make nicely formatted operators.
Resource functions defined using ResourceFunction["FormatAsResourceFunction"] will format nicely when undefined input patterns are given.

Examples

Basic Examples (5) 

Format a symbol as a ResourceFunction when it appears in output:

In[1]:=
MyFunction[x_][y_] := x + y;
ResourceFunction["FormatAsResourceFunction"][MyFunction];
MyFunction[5]
Out[3]=

Format using a different name:

In[4]:=
ResourceFunction["FormatAsResourceFunction"][MyFunction, "AnotherFunction"];
MyFunction[5]
Out[5]=

Format with an interpretation as a specific resource:

In[6]:=
ResourceFunction["FormatAsResourceFunction"][MyFunction, "CustomLabel", ResourceFunction["GrayCode"]];
MyFunction[5]
Out[7]=

Copy and paste the output above:

In[8]:=
ResourceFunction["GrayCode"][5]
Out[8]=

This is the same as:

In[9]:=
ResourceFunction["GrayCode"][5]
Out[9]=

Applications (5) 

Here is a function that has an unevaluated operator form:

In[10]:=
TestFunction[x_][y_] := x + y;
In[11]:=
TestFunction[5]
Out[11]=
In[12]:=
%[3]
Out[12]=

Although it works correctly as a ResourceFunction, the operator form is represented by the internal symbol:

In[13]:=
rf = DefineResourceFunction[TestFunction]
Out[13]=
In[14]:=
rf[5]
Out[14]=
In[15]:=
%[3]
Out[15]=

Format the symbol so it will appear nicely in output:

In[16]:=
ResourceFunction["FormatAsResourceFunction"][TestFunction];
rf = DefineResourceFunction[TestFunction]
Out[17]=

Now the operator formats as a ResourceFunction:

In[18]:=
rf[5]
Out[18]=
In[19]:=
%[3]
Out[19]=

Copy the output from above and paste into a Hold to see the interpretation:

In[20]:=
Hold[ResourceFunction["TestFunction"][5]] // InputForm
Out[20]=

Possible Issues (3) 

Attempting to copy and paste the formatted symbol before the ResourceFunction exists will result in an error:

In[21]:=
NotYetAResourceFunction[x_][y_] := x + y;
ResourceFunction["FormatAsResourceFunction"][
  NotYetAResourceFunction];
NotYetAResourceFunction[5]
Out[23]=

Copy and paste the output from above:

In[24]:=
ResourceFunction["NotYetAResourceFunction"][5][3]
Out[24]=

Paste the same again, but this time define the ResourceFunction first and it will work:

In[25]:=
DefineResourceFunction[NotYetAResourceFunction];
ResourceFunction["NotYetAResourceFunction"][5][3]
Out[26]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 08 February 2019

Related Resources

License Information