"ExprStruct" (Data Structure)
"ExprStruct"
represents an expression that can be modified without evaluating.
Details
- An "ExprStruct" is useful for working with Wolfram Language expressions without them evaluating.
- An "ExprStruct" makes immutable modifications, always returning the result as a new data structure:
-
CreateDataStructure["ExprStruct",expr] create a new "ExprStruct" containing expr Typed[x,"ExprStruct"] give x the type "ExprStruct" - Import format "WL" with an element of "ExprStructs" creates "ExprStruct" data structures.
- Import formats "WXF" and "MX" with an element of "ExprStruct" create "ExprStruct" data structures.
- For a data structure of type "ExprStruct", the following operations can be used:
-
ds["Apply",fun] returns an expr struct where the head of the expression is replaced by fun time: O(n) ds["ConstructWith",fun] returns a new expr struct where fun is applied to the expression time: O(1) ds["Depth"] the maximum number of indices to specify any part of the expression time: O(1) ds["Drop",i] returns a new expr struct with the ithelement dropped time: O(n) ds["Drop",i,j] returns a new expr struct with elements between i and j dropped time: O(n) ds["Evaluate"] returns a new expr struct of the evaluation the expression ds["Fold",fun] applies fun to the elements of the expression, accumulating a result time: O(n) ds["Fold",fun,init] applies fun to the elements of the expression, starting with init, accumulating a result time: O(n) ds["Get"] returns the expression held by ds time: O(1) ds["Head"] returns a new expr struct containing the head of the expression time: O(1) ds["Insert",x,i] returns a new expr struct with x inserted at position i time: O(n) ds["Length"] the number of elements stored in the expression time: O(1) ds["Map",fun] returns a new expr struct, which applies fun to each element of the expression time: O(n) ds["MapImmediateEvaluate",fun] returns a new expr struct, which applies fun to each element of the expression and evaluates it time: O(n) ds["Part",i] returns a new expr struct of the ith part of the expression time: O(1) ds["ReplacePart",ix] returns a new expr struct where the ith part of the expression is replaced with x time: O(n) ds["Visualization"] return a visualization of ds time: O(n) - The following functions are also supported:
-
dsi===dsj True, if dsi equals dsj FullForm[ds] full form of ds Information[ds] information about ds InputForm[ds] input form of ds Length[ds] length of ds Normal[ds] convert ds to a normal expression
Examples
open all close allBasic Examples (2)
A new "ExprStruct" can be created with CreateDataStructure:
ds = CreateDataStructure["ExprStruct", f[1, 2, 3]]Return a new "ExprStruct" that wraps the expression with h:
ds1 = ds["ConstructWith", h]Return the expression held in the new expr struct:
ds1["Get"]The original "ExprStruct" is unchanged:
ds["Get"]Create a new "ExprStruct" data structure:
ds = CreateDataStructure["ExprStruct", Nest[f, x, 20]]The length of the expression held by ds:
ds["Depth"]A visualization of the data structure:
ds["Visualization"]Scope (20)
Information (1)
A new "ExprStruct" can be created with CreateDataStructure:
ds = CreateDataStructure["ExprStruct", f[1]]Information about the data structure ds:
Information[ds]Import (4)
Import of "WL" format using an element of "ExprStructs" creates "ExprStruct" data structures:
Import["ExampleData/Collatz.m", "ExprStructs"]Use Import to load a string into an "ExprStruct":
ds = ImportString["Print[1]", {"WL", "ExprStructs", 1}]The expression is held without evaluation:
ds["ConstructWith", Hold]["Get"]Import of "WXF" format using an "ExprStruct" element creates an "ExprStruct" data structure:
bytes = BinarySerialize[Unevaluated[1 + 1]]
ds = ImportByteArray[bytes, {"WXF", "ExprStruct"}]The expression is held without evaluation:
ds["ConstructWith", Hold]["Get"]Import of "MX" format using an "ExprStruct" element creates an "ExprStruct" data structure:
str = ExportString[f[1], "MX"];
ds = ImportString[str, {"MX", "ExprStruct"}]Return the original expression:
ds["Get"]Operations (15)
"Apply" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y]]Return a new "ExprStruct" where the head of the expression is replaced by g:
ds1 = ds["Apply", g]Visualize the newly created data structure:
ds1["Visualization"]"ConstructWith" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y]]Return a new "ExprStruct" where g is applied to the expression:
ds1 = ds["ConstructWith", g]Visualize the newly created data structure:
ds1["Visualization"]"Depth" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y]]Get the depth of the stored expression:
ds["Depth"]"Drop" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y, z]];
ds["Visualization"]Drop the second element of the expression, returning a new "ExprStruct":
ds1 = ds["Drop", 2]ds1["Visualization"]"Evaluate" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", Unevaluated[Plus[4, 2]]];
ds["Visualization"]Return a new "ExprStruct" of the evaluation of the expression:
ds1 = ds["Evaluate"]Visualize the resulting expression:
ds1["Visualization"]"Fold" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[1, 2, 3, 4]];
ds["Visualization"]Combine all elements of the expression using Plus, producing their total sum:
ds["Fold", Plus]You can also specify an initial value to start the accumulation:
ds["Fold", Plus, 42]"Get" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y]]Get the contents stored in the data structure:
ds["Get"]Normal returns the same expression:
Normal[ds]"Head" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y]]Get the head of the expression as a new "ExprStruct":
ds1 = ds["Head"]Get the contents of the resulting expression:
ds1["Get"]"Insert" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y]]Insert an element at the specified position, returning a new "ExprStruct":
ds1 = ds["Insert", z, 2]Visualize the contents of the new data structure:
ds1["Visualization"]"Length" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y]]Get the number of elements in the expression:
ds["Length"]Length gives the same value:
Length[ds]"Map" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y, z]]Return a new "ExprStruct" where Print was applied to each element of the expression:
ds1 = ds["Map", Print]Visualize the resulting expression:
ds1["Visualization"]"MapImmediateEvaluate" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y, z]]Return a new "ExprStruct" where Print was applied to each element of the expression and evaluated:
ds1 = ds["MapImmediateEvaluate", Print]Visualize the resulting expression:
ds1["Visualization"]"Part" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y, z]]Get the second element of the expression as an "ExprStruct":
ds1 = ds["Part", 2]Visualize the resulting expression:
ds1["Visualization"]"ReplacePart" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y, z]]Return a new "ExprStruct" where the second part of the expression is replaced with g:
ds1 = ds["ReplacePart", 2, g]Visualize the resulting expression:
ds1["Visualization"]"Visualization" (1)
Create a new "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y, z]]Visualize the contents of the data structure:
ds["Visualization"]Applications (1)
Unevaluated Expressions (1)
An "ExprStruct" is useful for working with unevaluated expressions:
ds = CreateDataStructure["ExprStruct", f[1, 2]]Replace the head with Plus:
ds = ds["Apply", Plus]The underlying expression does not evaluate:
ds["Visualization"]Applying f to each element creates a new "ExprStruct":
ds["Map", f]["Visualization"]However, the "Map" operation does not evaluate on application:
ds["Map", # ^ 2&]["Visualization"]To evaluate each element after application, use the "MapImmediateEvaluate" operation:
ds["MapImmediateEvaluate", # ^ 2&]["Visualization"]Properties & Relations (4)
InputForm (1)
InputForm returns the serialized contents of the "ExprStruct":
InputForm[CreateDataStructure["ExprStruct", f[x]]]This serialized form can be used to recreate the data structure:
DataStructure["ExprStruct", {"Data" :> f[x]}]Length (1)
Length can be used to get the length of the expression stored inside the "ExprStruct":
ds = CreateDataStructure["ExprStruct", {x, y, z}];
Length[ds]The same can be achieved with the "Length" operation:
ds["Length"]Normal (1)
Normal can be used to get the contents of a "ExprStruct":
ds = CreateDataStructure["ExprStruct", f[x, y]];
Normal[ds]The same can be achieved with the "Get" operation:
ds["Get"]SameQ (1)
SameQ can be used to test whether two "ExprStruct" data structures have the same contents:
ds1 = CreateDataStructure["ExprStruct", f[x]];
ds2 = CreateDataStructure["ExprStruct", f[x]];
ds1 === ds2If one element is changed, the data structures are no longer the same:
ds1["ConstructWith", g];
ds1 === ds2Related Guides
History
Introduced in 2020 (12.1)