"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] 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 Normal[ds] convert ds to a normal expression
Examples
open allclose allBasic Examples (2)
A new "ExprStruct" can be created with CreateDataStructure:
The length of the expression held by ds:
Return a new "ExprStruct" that wraps the expression with h:
Return the expression held in the new expr struct:
Scope (4)
Information (1)
A new "ExprStruct" can be created with CreateDataStructure:
Import (3)
Import of "WL" format using an element of "ExprStructs" creates "ExprStruct" data structures:
Use Import to load a string into an "ExprStruct":
The expression is held without evaluation:
Import of "WXF" format using an "ExprStruct" element creates an "ExprStruct" data structure:
The expression is held without evaluation:
Import of "MX" format using an "ExprStruct" element creates an "ExprStruct" data structure:
Applications (1)
Unevaluated Expressions (1)
An "ExprStruct" is useful for working with unevaluated expressions. Create a new "ExprStruct":
Replace the head with Plus:
The underlying expression does not evaluate:
Applying f to each element creates a new expression:
However, the "Map" operation does not evaluate on application:
To evaluate each element after application, use the "MapImmediateEvaluate" operation: