Function Repository Resource:

ExpressionTransform

Source Notebook

Construct and aggregate subexpressions with descending and ascending operators

Contributed by: Ian Ford (Wolfram Research)

ResourceFunction["ExpressionTransform"][expr,f,g,n]

successively applies f to expr up to level n, applying g to each subexpression and its result.

ResourceFunction["ExpressionTransform"][expr,f,g,n,h]

applies g to h[expr] instead of expr.

ResourceFunction["ExpressionTransform"][expr,f,{g,g-1},n,h]

applies g-1 at the last level and g at each inner level.

ResourceFunction["ExpressionTransform"][expr,{f0,f1,},{g0,g1,,g-1},n,h]

applies the operators fi and gi at level i.

ResourceFunction["ExpressionTransform"][expr,<|i1fi1,i2fi2,|>,<|j1gj1,j2gj2,|>,n,h]

applies the operators fi and gi at level i.

Details and Options

ResourceFunction["ExpressionTransform"] can perform complex construction, filtering, reorganization, aggregation and transformation of arbitrary expressions.
ResourceFunction["ExpressionTransform"] can be seen as a generalization of Query that allows arbitrary expressions to be constructed and arbitrary operations to be applied at every level.
In ResourceFunction["ExpressionTransform"][expr,{f0,},{g0,,g-1},], the operators fi and gi are applied at successively deeper levels, but the fi are applied while "descending" while the gi are applied while "ascending".
A "descending" operator is applied to the expression before subsequent operators are applied at deeper levels. Descending operators can filter the parts of an expression at a given level without changing the structure of deeper levels, or they can be arbitrary operators giving arbitrary structures:
An "ascending" operator is applied to both the original expression and the result after all subsequent operators have been applied to deeper levels. Ascending operators can use values computed from prior applications of descending operators as well as the results of ascending operators from deeper levels:
In ResourceFunction["ExpressionTransform"][expr,f,g,n], n can be any non-negative machine integer or Infinity.
ResourceFunction["ExpressionTransform"] successively applies a descending operator f until the maximum level is reached or f returns an expression with no arguments.
In ResourceFunction["ExpressionTransform"][expr,f,g,n,h], h[expr] is used as the first argument of g rather than expr.
ResourceFunction["ExpressionTransform"][expr,{f0,f1,},{g0,g1,,g-1},n,h] gives the result of applying g0 to both h[expr] and expr0[res1,res2,], where f0[expr] gives expr0[expr1,expr2,] and ResourceFunction["ExpressionTransform"][expri,{f1,},{g1,,g-1},n-1,h] gives resi.
With the option setting HeadsTrue, ResourceFunction["ExpressionTransform"] includes heads of expressions and their parts.
ResourceFunction["ExpressionTransform"][expr,f,{,g-1},n,h] gives g-1[h[expr]] if n is 0 or f[expr] is atomic.
If f[expr] gives s[expr1,expr2,] where s is a symbol that holds its arguments, then the expressions expri are given to the functions f and h unevaluated in ResourceFunction["ExpressionTransform"][expr,f,g,n,h].
ResourceFunction["ExpressionTransform"][expr,f,g] is equivalent to ResourceFunction["ExpressionTransform"][expr,f,g,1], which gives g[expr,f[expr]].
If f[expr] gives <|key1expr1,key2expr2,|>, then ResourceFunction["ExpressionTransform"][expr,f,g,n] gives g[expr,<|key1res1,key2res2,|>], where resi is the result of ResourceFunction["ExpressionTransform"][expri,f,g,n-1].

Examples

Basic Examples (6) 

Nest a descending and an ascending operator twice:

In[1]:=
ResourceFunction["ExpressionTransform"][x, f, g, 2]
Out[1]=

Apply a descending and an ascending operator at successive levels:

In[2]:=
ResourceFunction["ExpressionTransform"][1, Range[3 # - 1, 3 # + 1] &, g, 2]
Out[2]=

Apply an ascending operator at the last level:

In[3]:=
ResourceFunction["ExpressionTransform"][1, Range[3 # - 1, 3 # + 1] &, {g, gl}, 2]
Out[3]=

Construct and visualize a tree as subscripted data with highlighted leaves, adding frames around inner subtrees:

In[4]:=
ResourceFunction["ExpressionTransform"][1, Range[3 # - 1, 3 # + 1] &, {Framed@*Subscript, Highlighted}, 2]
Out[4]=

Apply descending operators to the values of an association:

In[5]:=
ResourceFunction[
 "ExpressionTransform"][1, <|"Even" -> 2 #, "Odd" -> 2 # + 1|> &, {Framed@*Subscript, Highlighted}, 2]
Out[5]=

Apply descending operators to the arguments of an arbitrary expression:

In[6]:=
ResourceFunction["ExpressionTransform"][1, f[2 #, 2 # + 1] &, {Framed@*Subscript, Highlighted}, 2]
Out[6]=

Apply successive operators at successive levels:

In[7]:=
fs = {Echo@*f0, Echo@*f1, Echo@*f2};
gs = {Echo@*g0, Echo@*g1, Echo@*g2, Echo@*gl};
In[8]:=
ResourceFunction["ExpressionTransform"][x, fs, gs, 0]
Out[8]=
In[9]:=
ResourceFunction["ExpressionTransform"][x, fs, gs, 1]
Out[9]=
In[10]:=
ResourceFunction["ExpressionTransform"][x, fs, gs, 3]
Out[10]=

Apply a descending operator f at level 1 of an array:

In[11]:=
ResourceFunction[
 "ExpressionTransform"][{{a, b}, {c, d}}, <|1 -> f|>, <||>, Infinity]
Out[11]=

Apply Total afterward as an ascending operator at level 0:

In[12]:=
ResourceFunction[
 "ExpressionTransform"][{{a, b}, {c, d}}, <|1 -> f|>, <|
  0 -> (Total[#2] &)|>, Infinity]
Out[12]=

Apply Total first as a descending operator at level 0 instead:

In[13]:=
ResourceFunction[
 "ExpressionTransform"][{{a, b}, {c, d}}, <|0 -> Total, 1 -> f|>, <||>, Infinity]
Out[13]=

Scope (5) 

Apply a descending operator:

In[14]:=
ResourceFunction["ExpressionTransform"][x, f, {}]
Out[14]=

Apply an ascending operator:

In[15]:=
ResourceFunction["ExpressionTransform"][x, f, g]
Out[15]=

Apply operators at multiple levels:

In[16]:=
ResourceFunction["ExpressionTransform"][x, f, g, 3]
Out[16]=

Apply an ascending operator at the last level:

In[17]:=
ResourceFunction["ExpressionTransform"][x, f, {g, gl}, 3]
Out[17]=

Apply successive operators at successive levels:

In[18]:=
ResourceFunction[
 "ExpressionTransform"][x, {f0, f1, f2}, {g0, g1, g2, gl}, 3]
Out[18]=

Apply ascending operators to a property of the expression:

In[19]:=
ResourceFunction["ExpressionTransform"][x, f, {g, gl}, 3, h]
Out[19]=

Atomic expressions:

In[20]:=
ResourceFunction["ExpressionTransform"][a, Echo, {Echo@*g, Echo@*gl},
  2]
Out[20]=

Normal expressions:

In[21]:=
ResourceFunction["ExpressionTransform"][
 a[], Echo, {Echo@*g, Echo@*gl}, 2]
Out[21]=
In[22]:=
ResourceFunction["ExpressionTransform"][
 a[1, 2], Echo, {Echo@*g, Echo@*gl}, 2]
Out[22]=

Specify the number of additional levels:

In[23]:=
ResourceFunction["ExpressionTransform"][5, Range[# - 1] &, g, 2]
Out[23]=

Repeatedly apply descending operators until no more levels are added:

In[24]:=
ResourceFunction["ExpressionTransform"][5, Range[# - 1] &, g, Infinity]
Out[24]=

Apply operators without evaluating intermediate results:

In[25]:=
ResourceFunction["ExpressionTransform"][Unevaluated[3*2 + 1], Function[expr, Level[Unevaluated[expr], {1}, Hold], HoldFirst], g, 2, HoldForm]
Out[25]=

Apply subsequent operators to the values of an association:

In[26]:=
ResourceFunction[
 "ExpressionTransform"][1, <|"Even" -> 2 #, "Odd" -> 2 # + 1|> &, f,
  2]
Out[26]=

Options (2) 

Heads (2) 

By default, the operators are not applied to the heads:

In[27]:=
ResourceFunction["ExpressionTransform"][x, {a, b, c}, {}, 1]
Out[27]=
In[28]:=
ResourceFunction["ExpressionTransform"][x, {a, b, c}, {}, 2]
Out[28]=
In[29]:=
ResourceFunction["ExpressionTransform"][x, {a, b, c}, {}, 3]
Out[29]=

With the option setting HeadsTrue, ExpressionTransform includes heads of expressions and their parts:

In[30]:=
ResourceFunction["ExpressionTransform"][x, {a, b, c}, {}, 2, Heads -> True]
Out[30]=
In[31]:=
ResourceFunction["ExpressionTransform"][x, {a, b, c}, {}, 2, Heads -> True]
Out[31]=
In[32]:=
ResourceFunction["ExpressionTransform"][x, {a, b, c}, {}, 3, Heads -> True]
Out[32]=

Applications (2) 

Count the number of subtrees at each level of a tree:

In[33]:=
f[{tree_, {counts___}}] :=
  {#, {counts, 0}} & /@ TreeChildren[tree];
In[34]:=
g[{_, {counts___}}, {res___}] :=
  Total[PadRight[{{counts, 1}, res}]];
gl[{_, {counts___}}] :=
  {counts, 1};
In[35]:=
ResourceFunction["ExpressionTransform"][{\!\(\*
GraphicsBox[
NamespaceBox["Trees",
DynamicModuleBox[{Typeset`tree = HoldComplete[
Tree[Null, {
Tree[Null, None], 
Tree[Null, {
Tree[Null, None]}], 
Tree[Null, {
Tree[Null, {
Tree[Null, None]}]}]}]]}, 
NamespaceBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.8660254037844387, 2.5352693433792695`}, {0., 1.6901795622528462`}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.8660254037844387, 2.5352693433792695`}, {
            0.8660254037844387, 1.6901795622528462`}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.8660254037844387, 2.5352693433792695`}, {
            1.7320508075688774`, 1.6901795622528462`}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.8660254037844387, 1.6901795622528462`}, {
            0.8660254037844387, 0.8450897811264231}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{1.7320508075688774`, 1.6901795622528462`}, {
            1.7320508075688774`, 0.8450897811264231}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{1.7320508075688774`, 0.8450897811264231}, {
            1.7320508075688774`, 0.}}]}}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], 
TagBox[InsetBox[
FrameBox["",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
             
BaseStyle->GrayLevel[0],
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->{1, 1},
RoundingRadius->4,
StripOnInput->False], {0.8660254037844387, 2.5352693433792695}],
"DynamicName",
BoxID -> "VertexID$1"], 
TagBox[InsetBox[
FrameBox["",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
             
BaseStyle->GrayLevel[0],
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->{1, 1},
RoundingRadius->0,
StripOnInput->False], {0., 1.6901795622528462}],
"DynamicName",
BoxID -> "VertexID$2"], 
TagBox[InsetBox[
FrameBox["",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
             
BaseStyle->GrayLevel[0],
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->{1, 1},
RoundingRadius->4,
StripOnInput->False], {0.8660254037844387, 1.6901795622528462}],
"DynamicName",
BoxID -> "VertexID$3"], 
TagBox[InsetBox[
FrameBox["",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
             
BaseStyle->GrayLevel[0],
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->{1, 1},
RoundingRadius->0,
StripOnInput->False], {0.8660254037844387, 0.8450897811264231}],
"DynamicName",
BoxID -> "VertexID$4"], 
TagBox[InsetBox[
FrameBox["",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
             
BaseStyle->GrayLevel[0],
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->{1, 1},
RoundingRadius->4,
StripOnInput->False], {1.7320508075688774, 1.6901795622528462}],
"DynamicName",
BoxID -> "VertexID$5"], 
TagBox[InsetBox[
FrameBox["",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
             
BaseStyle->GrayLevel[0],
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->{1, 1},
RoundingRadius->4,
StripOnInput->False], {1.7320508075688774, 0.8450897811264231}],
"DynamicName",
BoxID -> "VertexID$6"], 
TagBox[InsetBox[
FrameBox["",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
             
BaseStyle->GrayLevel[0],
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->{1, 1},
RoundingRadius->0,
StripOnInput->False], {1.7320508075688774, 0.}],
"DynamicName",
BoxID -> "VertexID$7"]}}]]],
AlignmentPoint->Center,
Axes->False,
AxesLabel->None,
AxesOrigin->Automatic,
AxesStyle->{},
Background->None,
BaseStyle->{},
BaselinePosition->Automatic,
ContentSelectable->Automatic,
DefaultBaseStyle->"TreeGraphics",
Epilog->{},
FormatType->StandardForm,
Frame->False,
FrameLabel->FormBox["False", StandardForm],
FrameStyle->{},
FrameTicks->None,
FrameTicksStyle->{},
GridLines->None,
GridLinesStyle->{},
ImageMargins->0.,
ImagePadding->All,
ImageSize->{48.073101543142556`, Automatic},
LabelStyle->{},
PlotLabel->None,
PlotRange->All,
PlotRangeClipping->False,
PlotRangePadding->Automatic,
PlotRegion->Automatic,
Prolog->{},
RotateLabel->True,
Ticks->Automatic,
TicksStyle->{}]\), {}}, f, {g, gl}, Infinity]
Out[35]=

Create a tree of terms in a linear recurrence:

In[36]:=
ResourceFunction["ExpressionTransform"][6, If[# > 2, {# - 1, # - 2}, None] &, {Tree[Total[TreeData /@ #2], #2] &, RulesTree[1] &}, Infinity]
Out[36]=

Properties and Relations (17) 

NestList gives a list of the results of successive applications of an operator f to an expression:

In[37]:=
NestList[f, x, 3]
Out[37]=

ExpressionTransform successively applies descending operators f to an expression before subsequent operators are applied at deeper levels:

In[38]:=
ResourceFunction["ExpressionTransform"][x, List@*f, g, 3]
Out[38]=

ExpressionTransform applies subsequent operators to each part of an expression:

In[39]:=
ResourceFunction["ExpressionTransform"][x, {f1[#], f2[#]} &, g, 3]
Out[39]=

Fold gives the result of successively applying a binary operator g to the elements of a list:

In[40]:=
Fold[g, x, {a, b, c}]
Out[40]=

ExpressionTransform successively applies ascending binary operators g to an expression and the result after all subsequent operators have been applied at deeper levels:

In[41]:=
ResourceFunction["ExpressionTransform"][{a, b, c}, List@*Most, ReverseApplied[g], 3]
Out[41]=

ExpressionTransform applies subsequent operators to each part of an expression:

In[42]:=
ResourceFunction["ExpressionTransform"][{a, b, c}, Subsequences[#, {Length[#] - 1}] &, ReverseApplied[g], 3]
Out[42]=

ExpressionTransform applies subsequent operators to each part of an expression:

In[43]:=
ResourceFunction["ExpressionTransform"][x, f, g, 2]
Out[43]=
In[44]:=
ResourceFunction["ExpressionTransform"][x, List@*f, g, 2]
Out[44]=

NestList constructs a list by successively applying an operation f to an expression:

In[45]:=
NestList[f, x, 3]
Out[45]=

Fold successively applies a binary operation g to the elements of a list:

In[46]:=
Fold[g, Reverse[%]]
Out[46]=

ExpressionTransform constructs an expression from the top-down by successively applying descending operators f and applying ascending binary operators g to subexpressions from the bottom-up in a single traversal:

In[47]:=
ResourceFunction["ExpressionTransform"][x, List@*f, ReverseApplied[g],
  3]
Out[47]=

TreeFold[g,NestTree[f,Tree[expr,None],n,h]] is generally equivalent to ExpressionTransform[expr,f,g,n,h]:

In[48]:=
NestTree[{2 #, 2 # + 1} &, 1, 3, h]
Out[48]=
In[49]:=
TreeFold[g, %]
Out[49]=
In[50]:=
% === ResourceFunction["ExpressionTransform"][1, {2 #, 2 # + 1} &, g, 3, h]
Out[50]=

MapAll[g,expr] is generally equivalent to ExpressionTransform[expr,{},g[#2]&,g},Infinity]:

In[51]:=
expr = Groupings[Range[3], f -> 2]
Out[51]=
In[52]:=
MapAll[Framed, expr]
Out[52]=
In[53]:=
% === ResourceFunction["ExpressionTransform"][
  expr, {}, {Framed[#2] &, Framed}, Infinity]
Out[53]=

In ExpressionTransform, the ascending operator g takes the original expression as an argument for inner levels:

In[54]:=
ResourceFunction[
 "ExpressionTransform"][expr, {}, {Framed[Column[{#, #2}]] &, Framed}, Infinity]
Out[54]=

Apply Total as a descending operator:

In[55]:=
ResourceFunction[
 "ExpressionTransform"][{{a, b}, {c, d}}, <|0 -> Total, 1 -> f|>, <||>, Infinity]
Out[55]=

Apply Total as an ascending operator:

In[56]:=
ResourceFunction[
 "ExpressionTransform"][{{a, b}, {c, d}}, <|1 -> f|>, <|
  0 -> (Total[#2] &)|>, Infinity]
Out[56]=

Query applies either a descending operator or an ascending operator at each level:

In[57]:=
data = {
   <|"a" -> 1, "b" -> "x", "c" -> {1}|>,
   <|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>,
   <|"a" -> 3, "b" -> "z", "c" -> {3}|>,
   <|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>,
   <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
   <|"a" -> 6, "b" -> "z", "c" -> {}|>};
In[58]:=
Query[g0, "a", g]@data
Out[58]=

This can also be done with ExpressionTransform:

In[59]:=
ResourceFunction[
 "ExpressionTransform"][data, {Identity, Extract[{{"a"}}], Identity}, {g0[#2] &, First[#2] &, g}, 3]
Out[59]=

ExpressionTransform applies both a descending and an ascending operator at each level:

In[60]:=
ResourceFunction[
 "ExpressionTransform"][data, {EchoLabel[f0], EchoLabel[f1]@*Extract[{{Key["a"]}}], EchoLabel[f2]}, {Echo[g0[#2], g0] &, Echo[First[#2], g1] &, EchoLabel[g]@*g}, 3]
Out[60]=

ExpressionTransform[expr,f,g,0] gives expr:

In[61]:=
ResourceFunction["ExpressionTransform"][expr, f, g, 0]
Out[61]=

ExpressionTransform[expr,f,{,g-1},0] applies the operator g-1 to expr instead:

In[62]:=
ResourceFunction["ExpressionTransform"][expr, f, {g, gl}, 0]
Out[62]=

ExpressionTransform[expr,f,g] gives expr if f[expr] gives an atomic expression:

In[63]:=
ResourceFunction["ExpressionTransform"][expr, x &, g]
Out[63]=

ExpressionTransform[expr,f,{,g-1}] applies the operator g-1 to expr instead:

In[64]:=
ResourceFunction["ExpressionTransform"][expr, x &, {g, gl}]
Out[64]=

ExpressionTransform[expr,f,g] applies the operator g if f[expr] gives a normal expression:

In[65]:=
ResourceFunction["ExpressionTransform"][expr, f, g]
Out[65]=

This is true even if f[expr] gives an expression with no arguments:

In[66]:=
ResourceFunction["ExpressionTransform"][expr, {} &, g]
Out[66]=

ExpressionTransform successively applies a descending operator f until the maximum level is reached or f returns an expression with no arguments:

In[67]:=
ResourceFunction["ExpressionTransform"][5, Range[# - 1] &, g, Infinity]
Out[67]=

ExpressionTransform[expr,f,g,n,h] gives the result of applying g to both h[expr] and expr0[res1,res2,], where f[expr] gives expr0[expr1,expr2,] and ExpressionTransform[expri,f,g,n-1,h] gives resi:

In[68]:=
f[x_] := If[x > 7, {}, {2 x, 2 x + 1}]
In[69]:=
ResourceFunction["ExpressionTransform"][1, f, g, 2]
Out[69]=
In[70]:=
% === g[1, {ResourceFunction["ExpressionTransform"][2, f, g, 1], ResourceFunction["ExpressionTransform"][3, f, g, 1]}]
Out[70]=

ExpressionTransform[expr,{f0,f1,},{g0,g1,,g-1},n,h] gives the result of applying g0 to both h[expr] and expr0[res1,res2,], where f0[expr] gives expr0[expr1,expr2,] and ExpressionTransform[expri,{f1,},{g1,,g-1},n-1,h] gives resi:

In[71]:=
ResourceFunction[
 "ExpressionTransform"][expr, {f0, f1, f2}, {g0, g1, g2, gl}, 3, h]
Out[71]=
In[72]:=
% === g0[h[expr], f0[ResourceFunction["ExpressionTransform"][
    expr, {f1, f2}, {g1, g2, gl}, 2, h]]]
Out[72]=

ExpressionTransform[expr,{f},g,n,h] is equivalent to ExpressionTransform[expr,f,g,n,h]:

In[73]:=
ResourceFunction["ExpressionTransform"][expr, {f}, g, 3, h]
Out[73]=
In[74]:=
% === ResourceFunction["ExpressionTransform"][expr, f, g, 3, h]
Out[74]=

ExpressionTransform[expr,{},g,n,h] is equivalent to ExpressionTransform[Identity,g,n,h]:

In[75]:=
ResourceFunction[
 "ExpressionTransform"][{{a[1], b[2]}, {c[3], d[4]}}, {}, g, 3, h]
Out[75]=
In[76]:=
% === ResourceFunction[
  "ExpressionTransform"][{{a[1], b[2]}, {c[3], d[4]}}, Identity, g, 3,
   h]
Out[76]=

ExpressionTransform[expr,f,g,n,h] is equivalent to ExpressionTransform[expr,f,{g,Identity},n,h]:

In[77]:=
ResourceFunction["ExpressionTransform"][expr, f, g, 3, h]
Out[77]=
In[78]:=
% === ResourceFunction["ExpressionTransform"][expr, f, {g, Identity}, 3, h]
Out[78]=

ExpressionTransform[expr,f,{g},n,h] is equivalent to ExpressionTransform[expr,f,{#2&,g},n,h]:

In[79]:=
ResourceFunction["ExpressionTransform"][expr, f, {g}, 3, h]
Out[79]=
In[80]:=
% === ResourceFunction["ExpressionTransform"][expr, f, {#2 &, g}, 3, h]
Out[80]=

ExpressionTransform[expr,f,{},n,h] is equivalent to ExpressionTransform[expr,f,{#2&,Identity},n,h]:

In[81]:=
ResourceFunction["ExpressionTransform"][expr, f, {}, 3, h]
Out[81]=
In[82]:=
% === ResourceFunction["ExpressionTransform"][expr, f, {#2 &, Identity}, 3, h]
Out[82]=

ExpressionTransform[expr,{},{},Infinity] is generally equivalent to expr:

In[83]:=
ResourceFunction[
 "ExpressionTransform"][{{a[1], b[2]}, {c[3], d[4]}}, {}, {}, Infinity]
Out[83]=

Neat Examples (5) 

Construct the 3rd-step Sierpiński triangle:

In[84]:=
ResourceFunction["ExpressionTransform"][CirclePoints@3, Array[1/2 (KroneckerDelta[#1, #3] + KroneckerDelta[#2, #3]) &, {3, 3,
      3}] . # &, {RegionUnion @@ #2 &, Region@*Triangle}, 3]
Out[84]=

Construct the 3rd-step Cantor set:

In[85]:=
ResourceFunction["ExpressionTransform"][{0, 1}, Array[1/3 (2 KroneckerDelta[#1, #3] + KroneckerDelta[#2, #3]) &, {2, 2, 2}] . # &, {RegionUnion @@ #2 &, Region@*Interval}, 3]
Out[85]=

Define functions to compute branches of a fractal:

In[86]:=
lines = {};
In[87]:=
children[{z_, v_}] := Map[{z + #, #} &, v {Sqrt[3] + I, Sqrt[3] - I}/4]
In[88]:=
branch[xy_, res_] := (lines = Join[lines, Line[{xy, #}] & /@ res]; xy)

Construct a graphical image of the lines connecting the constructed points in the 0th- through 7th steps:

In[89]:=
ResourceFunction["ExpressionTransform"][{0, I}, children, branch, 7, ReIm@*First]
Out[89]=
In[90]:=
Graphics[lines]
Out[90]=

Define a function that divides a list with two or more elements into a left half and a right half:

In[91]:=
divide[{e_}] := None
divide[list_] := TakeDrop[list, Ceiling[Length[list]/2]]

Define a function that merges two sorted lists giving a sorted list:

In[92]:=
merge[_, {l_, r_}] := zip[{}, l, r]
In[93]:=
zip[{es___}, {l_, ls___}, {r_, rs___}] := If[l <= r, zip[{es, l}, {ls}, {r, rs}], zip[{es, r}, {l, ls}, {rs}]]
zip[{es___}, {ls___}, {}] := {es, ls}
zip[{es___}, {}, {rs___}] := {es, rs}

Perform a merge sort on a random list:

In[94]:=
list = RandomInteger[10, 15]
Out[94]=
In[95]:=
ResourceFunction["ExpressionTransform"][list, divide, merge, Infinity]
Out[95]=

Define a function that divides a non-empty list into the list of elements less than or equal to a pivot element and the list of elements greater than the pivot element:

In[96]:=
divide[{}] := None
divide[list_] := pivot[{}, Last[list], {}, Most[list]]
In[97]:=
pivot[{ls___}, p_, {rs___}, {e_, es___}] := If[e <= p, pivot[{ls, e}, p, {rs}, {es}], pivot[{ls}, p, {e, rs}, {es}]]
pivot[l_, p_, r_, {}] := {l, r}

Define a function that merges the results of sorting these lists:

In[98]:=
merge[{___, p_}, {{ls___}, {rs___}}] := {ls, p, rs}

Perform a quick sort on a random list:

In[99]:=
list = RandomInteger[10, 15]
Out[99]=
In[100]:=
ResourceFunction["ExpressionTransform"][list, divide, merge, Infinity]
Out[100]=

Version History

  • 1.0.2 – 03 February 2023
  • 1.0.1 – 13 January 2023
  • 1.0.0 – 06 January 2023

Source Metadata

Related Resources

License Information