Function Repository Resource:

LocalizedByRules

Source Notebook

Use rules as local variable lists

Contributed by: Yong-an Lu

ResourceFunction["LocalizedByRules"][rules,expr]

use rules as local variable lists in expr.

Details and Options

If the rules are given in nested lists, it is effectively mapped onto the inner lists.
The following options can be given:
MethodWithmethod to use for localizing
Possible explicit settings for the Method option include: Module, Block and With.
Left-hand side in rules can be wrapped by HoldPattern.

Examples

Basic Examples (2) 

Use rules as local variable lists:

In[1]:=
ResourceFunction["LocalizedByRules"][{n -> 2, e :> RandomReal[]}, Table[e, n]]
Out[1]=

Give a single rule for a localized variable:

In[2]:=
ResourceFunction["LocalizedByRules"][x -> 7, x^2]
Out[2]=

Scope (1) 

Apply each rule separately:

In[3]:=
ResourceFunction["LocalizedByRules"][{{x -> 1}, {x -> 3}, {x -> 7}}, x + # &]
Out[3]=

Options (2) 

Method (2) 

Localize using different Wolfram Language scoping mechanisms:

In[4]:=
Table[ResourceFunction["LocalizedByRules"][x -> 0, scope -> Hold[x], Method -> scope], {scope, {With, Block, Module}}]
Out[4]=

Check this using explicit scoping:

In[5]:=
Table[scope[{x = 0}, scope -> Hold[x]], {scope, {With, Block, Module}}]
Out[5]=

Applications (1) 

An easy way to apply the solutions of an equation to TemplateExpression:

In[6]:=
eqn = {(10 x + y)/(10 y + z) == x/z, 1 <= {x, y, z} <= 9, x != z};
ResourceFunction["LocalizedByRules"][Solve[eqn, {x, y, z}, Integers],
  TemplateApply[
   "(10x+y)/(10y+z)=<*10x+y*>/<*10y+z*>=<*x*>/<*z*>=x/z, where x is <*x*>, y is <*y*>, z is <*z*>."],
  Method -> Block
  ] // Column
Out[5]=

Possible Issues (2) 

Variables with assigned values cannot be used in rules:

In[7]:=
a = 1;
ResourceFunction["LocalizedByRules"][a -> 2, a]
Out[5]=

One can use HoldPattern to keep the left-hand side unevaluated:

In[8]:=
ResourceFunction["LocalizedByRules"][HoldPattern[a] -> 2, a]
Out[8]=

The left hand side of each rule must be a symbol:

In[9]:=
ResourceFunction["LocalizedByRules"][{n -> 2, f[x_] :> x}, f[n]]
Out[9]=

Publisher

Yong-an Lu

Version History

  • 1.0.0 – 14 October 2019

Related Resources

License Information