"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 funtime: O(n)
    ds["ConstructWith",fun]returns a new expr struct where fun is applied to the expressiontime: O(1)
    ds["Depth"]the maximum number of indices to specify any part of the expressiontime: O(1)
    ds["Drop",i]returns a new expr struct with the ithelement droppedtime: O(n)
    ds["Drop",i,j]returns a new expr struct with elements between i and j droppedtime: 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 resulttime: O(n)
    ds["Fold",fun]applies fun to the elements of the expression, starting with init, accumulating a resulttime: O(n)
    ds["Get"]returns the expression held by dstime: O(1)
    ds["Head"]returns a new expr struct containing the head of the expressiontime: O(1)
    ds["Insert",x,i]returns a new expr struct with x inserted at position itime: O(n)
    ds["Length"]the number of elements stored in the expressiontime: O(1)
    ds["Map",fun]returns a new expr struct, which applies fun to each element of the expressiontime: O(n)
    ds["MapImmediateEvaluate",fun]returns a new expr struct, which applies fun to each element of the expression and evaluates ittime: O(n)
    ds["Part",i]returns a new expr struct of the ith part of the expressiontime: O(1)
    ds["ReplacePart",ix]returns a new expr struct where the ith part of the expression is replaced with xtime: O(n)
    ds["Visualization"]return a visualization of dstime: O(n)
  • The following functions are also supported:
  • dsi===dsjTrue, 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 all

Basic 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:

The original "ExprStruct" is unchanged:

A visualization of an "ExprStruct":

Scope  (4)

Information  (1)

A new "ExprStruct" can be created with CreateDataStructure:

Information about the data structure ds:

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:

Return the original expression:

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: