Function Repository Resource:

Contextualize

Source Notebook

Move symbols into a context

Contributed by: Bob Sandheinrich

ResourceFunction["Contextualize"][context,"name"]

gives the symbol context`name.

ResourceFunction["Contextualize"][context,symbol]

gives the symbol context`symbol.

ResourceFunction["Contextualize"][context,expr]

contextualizes symbols within an expression.

ResourceFunction["Contextualize"][context]

gives an operator for contextualizing expressions.

Details and Options

ResourceFunction["Contextualize"] accepts the following options:
"OnlyExistingNames"Truewhether to only apply context to names with existing symbols
"BlobFormatting"Automaticfunction to create a blob
ColorFunctionResourceFunction["HashHue"]color of the blob when using default blob formatting
The operator form is formatted as a small colorful blob.
The option ColorFunction is accepted and applied to the context when styling the operator blob.
By default, the blob coloring is determined by the resource function HashHue.
By default, only symbols that exist in the specified context are contextualized. Setting the option "OnlyExistingNames" to False will add the context to any symbol in either Global` or $Context.

Examples

Basic Examples (2) 

Define a function in a new context:

In[1]:=
MyContext`func[x_] := x + 2

Define the symbol using Contextualize:

In[2]:=
f = ResourceFunction["Contextualize"]["MyContext", "func"]
Out[2]=
Image

Use it:

In[3]:=
f[10]
Out[3]=
Image

Use a symbol name instead:

In[4]:=
ResourceFunction["Contextualize"]["MyContext", func]
Out[4]=
Image

Create a context blob:

In[5]:=
toMy = ResourceFunction["Contextualize"]["MyContext"]
Out[5]=
Image

Use the operator to get a symbol:

In[6]:=
toMy[func]
Out[6]=
Image

Apply the function using the operator:

In[7]:=
toMy[func][10]
Out[7]=
Image

Re-contextualize symbols with an expression:

In[8]:=
toMy[g[{func[10], func[-10]}]]
Out[8]=
Image

Deeper contexts work as well:

In[9]:=
This`Is`A`Long`ContextPath`$symb = 100;

Create a context blob for the long context:

In[10]:=
longC = ResourceFunction["Contextualize"][
  Context[This`Is`A`Long`ContextPath`$symb]]
Out[10]=
Image

Use it:

In[11]:=
longC[$symb]
Out[11]=
Image

Options (5) 

BlobFormatting (2) 

Customize a context blob:

In[12]:=
Stars`And`Stripes[] := "USA"
In[13]:=
starblob = ResourceFunction["Contextualize"]["Stars`And`", "BlobFormatting" -> (Tooltip[Framed["***"], #1] &)]
Out[13]=
Image

Use it:

In[14]:=
starblob[Stripes]
Out[14]=
Image
In[15]:=
%[]
Out[15]=
Image

ColorFunction (1) 

Choose the background color:

In[16]:=
toMy = ResourceFunction["Contextualize"]["MyContext", ColorFunction -> (Red &)]
Out[16]=
Image

OnlyExistingNames (2) 

By default, only existing symbols are contextualized:

In[17]:=
MyNewContext`func[x_] := x + 2
In[18]:=
ResourceFunction[
 "Contextualize"]["MyNewContext`", {func, newsymbol, Plot, otherc`foo}]
Out[18]=
Image

Set OnlyExistingNames to False to contextualize all symbols in $Context or Global` context:

In[19]:=
ResourceFunction[
 "Contextualize"]["MyNewContext`", {func, newsymbol, Plot, otherc`foo}, "OnlyExistingNames" -> False]
Out[19]=
Image

Version History

  • 1.0.0 – 29 June 2020

Related Resources

License Information