Sawmill

Class XElementExtensions

Extension methods for XElements.

Inheritance
Declaration
public static class XElementExtensions : Object

Methods

ChildrenInContext(XElement)

Returns an array containing each immediate child of value paired with a function to replace the child. This is typically useful when you need to replace a node's children one at a time, such as during mutation testing.

The replacement function can be seen as the "context" of the child; calling the function with a new child "plugs the hole" in the context.

SelfAndDescendantsInContext<T>(IRewriter<T>, T)DescendantsAndSelfInContext<T>(IRewriter<T>, T)

Declaration
public static ValueTuple<XElement, Func<XElement, XElement>>[] ChildrenInContext(this XElement value)
Parameters
Type Name Description

XElement

value

The value to get the contexts for the immediate children

Returns
Type Description

ValueTuple<XElement, Func<XElement, XElement>>[]

See Also
ChildrenInContext<T>(IRewriter<T>, T)

CountChildren(XElement)

Count the immediate children of the value. CountChildren()

Declaration
public static int CountChildren(this XElement value)
Parameters
Type Name Description

XElement

value

The value

Returns
Type Description

Int32

value's number of immediate children

Examples

Given a representation of the expression (1+2)+3,

Expr expr = new Add(
    new Add(
        new Lit(1),
        new Lit(2)
    ),
    new Lit(3)
);
CountChildren(T) counts the immediate children of the topmost (Add) node.
Assert.Equal(2, rewriter.CountChildren(expr));
See Also
CountChildren(T)

Cursor(XElement)

Create a Cursor<T>(IRewriter<T>, T) focused on the root node of value.

Declaration
public static Cursor<XElement> Cursor(this XElement value)
Parameters
Type Name Description

XElement

value

The root node on which the newly created Cursor<T>(IRewriter<T>, T) should be focused

Returns
Type Description

Cursor<XElement>

A Cursor<T>(IRewriter<T>, T) focused on the root node of value

See Also
Cursor<T>(IRewriter<T>, T)

DescendantAt(XElement, IEnumerable<Direction>)

Returns the descendant at a particular location in value

Declaration
public static XElement DescendantAt(this XElement value, IEnumerable<Direction> path)
Parameters
Type Name Description

XElement

value

The rewritable tree type

IEnumerable<Direction>

path

The route to take to find the descendant

Returns
Type Description

XElement

The descendant found by following the directions in path

Exceptions
Type Condition

InvalidOperationException

Thrown if path leads off the edge of the tree

See Also
DescendantAt<T>(IRewriter<T>, IEnumerable<Direction>, T)

DescendantsAndSelf(XElement)

Yields all of the nodes in the tree represented by value, starting at the bottom.

This is a depth-first post-order traversal.

SelfAndDescendants<T>(IRewriter<T>, T)

Declaration
public static IEnumerable<XElement> DescendantsAndSelf(this XElement value)
Parameters
Type Name Description

XElement

value

The value to traverse

Returns
Type Description

IEnumerable<XElement>

An enumerable containing all of the nodes in the tree represented by value, starting at the bottom.

Examples
  Expr expr = new Add(
      new Add(
          new Lit(1),
          new Lit(2)
      ),
      new Lit(3)
  );
  Expr[] expected = new[]
      {
          new Lit(1),
          new Lit(2),
          new Add(new Lit(1), new Lit(2)),
          new Lit(3),
          expr    
      };
  Assert.Equal(expected, rewriter.DescendantsAndSelf(expr));
See Also
DescendantsAndSelf<T>(IRewriter<T>, T)

DescendantsAndSelfInContext(XElement)

Yields each node in the tree represented by value paired with a function to replace the node, starting at the bottom. This is typically useful when you need to replace nodes one at a time, such as during mutation testing.

The replacement function can be seen as the "context" of the node; calling the function with a new node "plugs the hole" in the context.

This is a depth-first post-order traversal.

DescendantsAndSelf<T>(IRewriter<T>, T)ChildrenInContext<T>(IRewriter<T>, T)SelfAndDescendantsInContext<T>(IRewriter<T>, T)

Declaration
public static IEnumerable<ValueTuple<XElement, Func<XElement, XElement>>> DescendantsAndSelfInContext(this XElement value)
Parameters
Type Name Description

XElement

value

The value to get the contexts for the descendants

Returns
Type Description

IEnumerable<ValueTuple<XElement, Func<XElement, XElement>>>

See Also
DescendantsAndSelfInContext<T>(IRewriter<T>, T)

Fold<T>(XElement, SpanFunc<T, XElement, T>)

Flattens all the nodes in the tree represented by value into a single result, using an aggregation function to combine each node with the results of folding its children.

Declaration
public static T Fold<T>(this XElement value, SpanFunc<T, XElement, T> func)
Parameters
Type Name Description

XElement

value

The value to fold

SpanFunc<T, XElement, T>

func

The aggregation function

Returns
Type Description

T

The result of aggregating the tree represented by value.

Type Parameters
Name Description

T

See Also
Fold<T, U>(IRewriter<T>, SpanFunc<U, T, U>, T)
Fold<T, U>(T, SpanFunc<U, T, U>)

Fold<T>(XElement, Func<Memory<T>, XElement, ValueTask<T>>)

Flattens all the nodes in the tree represented by value into a single result, using an asynchronous aggregation function to combine each node with the results of folding its children.

Declaration
public static ValueTask<T> Fold<T>(this XElement value, Func<Memory<T>, XElement, ValueTask<T>> func)
Parameters
Type Name Description

XElement

value

The value to fold

Func<Memory<T>, XElement, ValueTask<T>>

func

The asynchronous aggregation function

Returns
Type Description

ValueTask<T>

The result of aggregating the tree represented by value.

Type Parameters
Name Description

T

Remarks

This method is not available on platforms which do not support ValueTask.

See Also
Fold<T, U>(IRewriter<T>, Func<Memory<U>, T, ValueTask<U>>, T)
Fold<T, U>(T, Func<Memory<U>, T, ValueTask<U>>)

GetChildren(XElement)

Get the immediate children of the value. GetChildren(Span<T>)

Declaration
public static XElement[] GetChildren(this XElement value)
Parameters
Type Name Description

XElement

value

The value

Returns
Type Description

XElement[]

The immediate children of value

Examples

Given a representation of the expression (1+2)+3,

Expr expr = new Add(
    new Add(
        new Lit(1),
        new Lit(2)
    ),
    new Lit(3)
);
GetChildren<T>(IRewriter<T>, T) returns the immediate children of the topmost node.
Expr[] expected = new[]
    {
        new Add(
            new Lit(1),
            new Lit(2)
        ),
        new Lit(3)
    };
Assert.Equal(expected, rewriter.GetChildren(expr));
See Also
GetChildren<T>(IRewriter<T>, T)

GetChildren(XElement, Span<XElement>)

Copy the immediate children of the value into childrenReceiver. GetChildren(Span<T>)

Declaration
public static void GetChildren(this XElement value, Span<XElement> childrenReceiver)
Parameters
Type Name Description

XElement

value

The value

Span<XElement>

childrenReceiver

A Span<T> to copy value's immediate children into. The Span<T>'s Length will be equal to the number returned by CountChildren(T).

Examples

Given a representation of the expression (1+2)+3,

Expr expr = new Add(
    new Add(
        new Lit(1),
        new Lit(2)
    ),
    new Lit(3)
);
GetChildren(Span<T>, T) copies the immediate children of the topmost node into the span.
Expr[] expected = new[]
    {
        new Add(
            new Lit(1),
            new Lit(2)
        ),
        new Lit(3)
    };
var array = new Expr[rewriter.CountChildren(expr)];
rewriter.GetChildren(array, expr);
Assert.Equal(expected, array);
See Also
GetChildren(Span<T>, T)

ReplaceDescendantAt<T>(XElement, IEnumerable<Direction>, XElement)

Replaces the descendant at a particular location in value

Declaration
public static XElement ReplaceDescendantAt<T>(this XElement value, IEnumerable<Direction> path, XElement newDescendant)
Parameters
Type Name Description

XElement

value

The rewritable tree type

IEnumerable<Direction>

path

The route to take to find the descendant

XElement

newDescendant

The replacement descendant

Returns
Type Description

XElement

A copy of value with newDescendant placed at the location indicated by path

Type Parameters
Name Description

T

Exceptions
Type Condition

InvalidOperationException

Thrown if path leads off the edge of the tree

See Also
ReplaceDescendantAt<T>(IRewriter<T>, IEnumerable<Direction>, T, T)

Rewrite(XElement, Func<XElement, ValueTask<XElement>>)

Rebuild a tree by applying an asynchronous transformation function to every node from bottom to top.

Declaration
public static ValueTask<XElement> Rewrite(this XElement value, Func<XElement, ValueTask<XElement>> transformer)
Parameters
Type Name Description

XElement

value

The value to rewrite

Func<XElement, ValueTask<XElement>>

transformer

The asynchronous transformation function to apply to every node in the tree

Returns
Type Description

ValueTask<XElement>

The result of applying transformer to every node in the tree represented by value.

Remarks

This method is not available on platforms which do not support ValueTask.

Examples

Given a representation of the expression (1+2)+3,

Expr expr = new Add(
    new Add(
        new Lit(1),
        new Lit(2)
    ),
    new Lit(3)
);
Rewrite<T>(IRewriter<T>, Func<T, ValueTask<T>>, T) replaces the leaves of the tree with the result of calling transformer,

then replaces their parents with the result of calling transformer, and so on. By the end, Rewrite<T>(IRewriter<T>, Func<T, ValueTask<T>>, T) has traversed the whole tree.

Expr expected = await transformer(new Add(
    await transformer(new Add(
        await transformer(new Lit(1)),
        await transformer(new Lit(2))
    )),
    await transformer(new Lit(3))
));
Assert.Equal(expected, await rewriter.Rewrite(transformer, expr));
See Also
Rewrite<T>(IRewriter<T>, Func<T, ValueTask<T>>, T)

Rewrite(XElement, Func<XElement, XElement>)

Rebuild a tree by applying a transformation function to every node from bottom to top.

Declaration
public static XElement Rewrite(this XElement value, Func<XElement, XElement> transformer)
Parameters
Type Name Description

XElement

value

The value to rewrite

Func<XElement, XElement>

transformer

The transformation function to apply to every node in the tree

Returns
Type Description

XElement

The result of applying transformer to every node in the tree represented by value.

Examples

Given a representation of the expression (1+2)+3,

Expr expr = new Add(
    new Add(
        new Lit(1),
        new Lit(2)
    ),
    new Lit(3)
);
Rewrite<T>(IRewriter<T>, Func<T, T>, T) replaces the leaves of the tree with the result of calling transformer,

then replaces their parents with the result of calling transformer, and so on. By the end, Rewrite<T>(IRewriter<T>, Func<T, T>, T) has traversed the whole tree.

Expr expected = transformer(new Add(
    transformer(new Add(
        transformer(new Lit(1)),
        transformer(new Lit(2))
    )),
    transformer(new Lit(3))
));
Assert.Equal(expected, rewriter.Rewrite(transformer, expr));
See Also
Rewrite<T>(IRewriter<T>, Func<T, T>, T)

RewriteChildren(XElement, Func<XElement, ValueTask<XElement>>)

Update the immediate children of the value by applying an asynchronous transformation function to each one.

Declaration
public static ValueTask<XElement> RewriteChildren(this XElement value, Func<XElement, ValueTask<XElement>> transformer)
Parameters
Type Name Description

XElement

value

The old value, whose immediate children should be transformed by transformer.

Func<XElement, ValueTask<XElement>>

transformer

An asynchronous transformation function to apply to each of value's immediate children.

Returns
Type Description

ValueTask<XElement>

A copy of value with updated children.

Remarks

This method is not available on platforms which do not support ValueTask.

See Also
RewriteChildren<T>(IRewriter<T>, Func<T, ValueTask<T>>, T)

RewriteChildren(XElement, Func<XElement, XElement>)

Update the immediate children of the value by applying a transformation function to each one.

Declaration
public static XElement RewriteChildren(this XElement value, Func<XElement, XElement> transformer)
Parameters
Type Name Description

XElement

value

The old value, whose immediate children should be transformed by transformer.

Func<XElement, XElement>

transformer

A transformation function to apply to each of value's immediate children.

Returns
Type Description

XElement

A copy of value with updated children.

See Also
RewriteChildren<T>(IRewriter<T>, Func<T, T>, T)

RewriteDescendantAt<T>(XElement, IEnumerable<Direction>, Func<XElement, ValueTask<XElement>>)

Apply an asynchronous function at a particular location in value

Declaration
public static ValueTask<XElement> RewriteDescendantAt<T>(this XElement value, IEnumerable<Direction> path, Func<XElement, ValueTask<XElement>> transformer)
Parameters
Type Name Description

XElement

value

The rewritable tree type

IEnumerable<Direction>

path

The route to take to find the descendant

Func<XElement, ValueTask<XElement>>

transformer

An asynchronous function to calculate a replacement for the descendant

Returns
Type Description

ValueTask<XElement>

A copy of value with the result of transformer placed at the location indicated by path

Type Parameters
Name Description

T

Remarks

This method is not available on platforms which do not support ValueTask.

Exceptions
Type Condition

InvalidOperationException

Thrown if path leads off the edge of the tree

See Also
RewriteDescendantAt<T>(IRewriter<T>, IEnumerable<Direction>, Func<T, ValueTask<T>>, T)

RewriteDescendantAt<T>(XElement, IEnumerable<Direction>, Func<XElement, XElement>)

Apply a function at a particular location in value

Declaration
public static XElement RewriteDescendantAt<T>(this XElement value, IEnumerable<Direction> path, Func<XElement, XElement> transformer)
Parameters
Type Name Description

XElement

value

The rewritable tree type

IEnumerable<Direction>

path

The route to take to find the descendant

Func<XElement, XElement>

transformer

A function to calculate a replacement for the descendant

Returns
Type Description

XElement

A copy of value with the result of transformer placed at the location indicated by path

Type Parameters
Name Description

T

Exceptions
Type Condition

InvalidOperationException

Thrown if path leads off the edge of the tree

See Also
RewriteDescendantAt<T>(IRewriter<T>, IEnumerable<Direction>, Func<T, T>, T)

RewriteIter(XElement, Func<XElement, ValueTask<XElement>>)

Rebuild a tree by repeatedly applying an asynchronous transformation function to every node in the tree, until a fixed point is reached. transformer should always eventually return its argument unchanged, or this method will loop. That is, x.RewriteIter(transformer).SelfAndDescendants().All(x => await transformer(x) == x).

This is typically useful when you want to put your tree into a normal form by applying a collection of rewrite rules until none of them can fire any more.

Declaration
public static ValueTask<XElement> RewriteIter(this XElement value, Func<XElement, ValueTask<XElement>> transformer)
Parameters
Type Name Description

XElement

value

The value to rewrite

Func<XElement, ValueTask<XElement>>

transformer

An asynchronous transformation function to apply to every node in value repeatedly.

Returns
Type Description

ValueTask<XElement>

The result of applying transformer to every node in the tree represented by value repeatedly until a fixed point is reached.

Remarks

This method is not available on platforms which do not support ValueTask.

See Also
RewriteIter<T>(IRewriter<T>, Func<T, ValueTask<T>>, T)

RewriteIter(XElement, Func<XElement, XElement>)

Rebuild a tree by repeatedly applying a transformation function to every node in the tree, until a fixed point is reached. transformer should always eventually return its argument unchanged, or this method will loop. That is, x.RewriteIter(transformer).SelfAndDescendants().All(x => transformer(x) == x).

This is typically useful when you want to put your tree into a normal form by applying a collection of rewrite rules until none of them can fire any more.

Declaration
public static XElement RewriteIter(this XElement value, Func<XElement, XElement> transformer)
Parameters
Type Name Description

XElement

value

The value to rewrite

Func<XElement, XElement>

transformer

A transformation function to apply to every node in value repeatedly.

Returns
Type Description

XElement

The result of applying transformer to every node in the tree represented by value repeatedly until a fixed point is reached.

See Also
RewriteIter<T>(IRewriter<T>, Func<T, T>, T)

SelfAndDescendants(XElement)

Yields all of the nodes in the tree represented by value, starting at the top.

This is a depth-first pre-order traversal.

DescendantsAndSelf<T>(IRewriter<T>, T)

Declaration
public static IEnumerable<XElement> SelfAndDescendants(this XElement value)
Parameters
Type Name Description

XElement

value

The value to traverse

Returns
Type Description

IEnumerable<XElement>

An enumerable containing all of the nodes in the tree represented by value, starting at the top.

Examples
  Expr expr = new Add(
      new Add(
          new Lit(1),
          new Lit(2)
      ),
      new Lit(3)
  );
  Expr[] expected = new[]
      {
          expr,
          new Add(new Lit(1), new Lit(2)),
          new Lit(1),
          new Lit(2),
          new Lit(3),
      };
  Assert.Equal(expected, rewriter.SelfAndDescendants(expr));
See Also
SelfAndDescendants<T>(IRewriter<T>, T)

SelfAndDescendantsBreadthFirst(XElement)

Yields all of the nodes in the tree represented by value in a breadth-first traversal order.

This is a breadth-first pre-order traversal.

Declaration
public static IEnumerable<XElement> SelfAndDescendantsBreadthFirst(this XElement value)
Parameters
Type Name Description

XElement

value

The value to traverse

Returns
Type Description

IEnumerable<XElement>

An enumerable containing all of the nodes in the tree represented by value in a breadth-first traversal order.

See Also
SelfAndDescendantsBreadthFirst<T>(IRewriter<T>, T)

SelfAndDescendantsInContext(XElement)

Yields each node in the tree represented by value paired with a function to replace the node, starting at the top. This is typically useful when you need to replace nodes one at a time, such as during mutation testing.

The replacement function can be seen as the "context" of the node; calling the function with a new node "plugs the hole" in the context.

This is a depth-first pre-order traversal.

SelfAndDescendants<T>(IRewriter<T>, T)ChildrenInContext<T>(IRewriter<T>, T)DescendantsAndSelfInContext<T>(IRewriter<T>, T)

Declaration
public static IEnumerable<ValueTuple<XElement, Func<XElement, XElement>>> SelfAndDescendantsInContext(this XElement value)
Parameters
Type Name Description

XElement

value

The value to get the contexts for the descendants

Returns
Type Description

IEnumerable<ValueTuple<XElement, Func<XElement, XElement>>>

See Also
SelfAndDescendantsInContext<T>(IRewriter<T>, T)

SelfAndDescendantsInContextBreadthFirst(XElement)

Yields each node in the tree represented by value paired with a function to replace the node, in a breadth-first traversal order. This is typically useful when you need to replace nodes one at a time, such as during mutation testing.

The replacement function can be seen as the "context" of the node; calling the function with a new node "plugs the hole" in the context.

This is a breadth-first pre-order traversal.

SelfAndDescendants<T>(IRewriter<T>, T)ChildrenInContext<T>(IRewriter<T>, T)DescendantsAndSelfInContext<T>(IRewriter<T>, T)

Declaration
public static IEnumerable<ValueTuple<XElement, Func<XElement, XElement>>> SelfAndDescendantsInContextBreadthFirst(this XElement value)
Parameters
Type Name Description

XElement

value

The value to get the contexts for the descendants

Returns
Type Description

IEnumerable<ValueTuple<XElement, Func<XElement, XElement>>>

See Also
SelfAndDescendantsInContextBreadthFirst<T>(IRewriter<T>, T)

SetChildren(XElement, ReadOnlySpan<XElement>)

Set the immediate children of the value.

Callers should ensure that newChildren contains the same number of children as was returned by GetChildren(Span<T>, T).

SetChildren(ReadOnlySpan<T>)

Declaration
public static XElement SetChildren(this XElement value, ReadOnlySpan<XElement> newChildren)
Parameters
Type Name Description

XElement

value

The old value, whose immediate children should be replaced

ReadOnlySpan<XElement>

newChildren

The new children

Returns
Type Description

XElement

A copy of value with updated children.

Examples

Given a representation of the expression (1+2)+3,

Expr expr = new Add(
    new Add(
        new Lit(1),
        new Lit(2)
    ),
    new Lit(3)
);
SetChildren(ReadOnlySpan<T>, T) replaces the immediate children of the topmost node.
Expr expected = new Add(
    new Lit(4),
    new Lit(5)
);
Assert.Equal(expected, rewriter.SetChildren(Children.Two(new Lit(4), new Lit(5)), expr));
See Also
SetChildren(ReadOnlySpan<T>, T)

ZipFold<U>(XElement, XElement, Func<XElement, XElement, IAsyncEnumerable<U>, ValueTask<U>>)

Flatten all of the nodes in the trees represented by values into a single value at the same time, using an aggregation function to combine nodes with the results of aggregating their children. The trees are iterated in lock-step, much like an n-ary Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>).

When trees are not the same size, the larger ones are truncated both horizontally and vertically. That is, if a pair of nodes have a different number of children, the rightmost children of the larger of the two nodes are discarded.

Declaration
public static ValueTask<U> ZipFold<U>(this XElement value1, XElement value2, Func<XElement, XElement, IAsyncEnumerable<U>, ValueTask<U>> func)
Parameters
Type Name Description

XElement

value1

XElement

value2

Func<XElement, XElement, IAsyncEnumerable<U>, ValueTask<U>>

func

The aggregation function

Returns
Type Description

ValueTask<U>

The result of aggregating the two trees

Type Parameters
Name Description

U

Remarks

This method is not available on platforms which do not support ValueTask and IAsyncEnumerable<T>.

Examples

Here's an example of using ZipFold<T, U>(IRewriter<T>, Func<T[], IAsyncEnumerable<U>, ValueTask<U>>, T[]) to test if two trees are syntactically equal.

static bool Equals(this Expr left, Expr right)
    => left.ZipFold<Expr, bool>(
        right,
        (xs, results) =>
        {
            switch (xs[0])
            {
                case Add a1 when xs[1] is Add a2:
                    return results.All(x => x);
                case Lit l1 when xs[1] is Lit l2:
                    return l1.Value == l2.Value;
                default:
                    return false;
            }
        }
    );
See Also
ZipFold<T, U>(IRewriter<T>, Func<T[], IAsyncEnumerable<U>, ValueTask<U>>, T[])

ZipFold<U>(XElement, XElement, Func<XElement, XElement, IEnumerable<U>, U>)

Flatten all of the nodes in the trees represented by values into a single value at the same time, using an aggregation function to combine nodes with the results of aggregating their children. The trees are iterated in lock-step, much like an n-ary Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>).

When trees are not the same size, the larger ones are truncated both horizontally and vertically. That is, if a pair of nodes have a different number of children, the rightmost children of the larger of the two nodes are discarded.

Declaration
public static U ZipFold<U>(this XElement value1, XElement value2, Func<XElement, XElement, IEnumerable<U>, U> func)
Parameters
Type Name Description

XElement

value1

XElement

value2

Func<XElement, XElement, IEnumerable<U>, U>

func

The aggregation function

Returns
Type Description

U

The result of aggregating the two trees

Type Parameters
Name Description

U

Examples

Here's an example of using ZipFold<T, U>(IRewriter<T>, Func<T[], IEnumerable<U>, U>, T[]) to test if two trees are syntactically equal.

static bool Equals(this Expr left, Expr right)
    => left.ZipFold<Expr, bool>(
        right,
        (xs, results) =>
        {
            switch (xs[0])
            {
                case Add a1 when xs[1] is Add a2:
                    return results.All(x => x);
                case Lit l1 when xs[1] is Lit l2:
                    return l1.Value == l2.Value;
                default:
                    return false;
            }
        }
    );
See Also
ZipFold<T, U>(IRewriter<T>, Func<T[], IEnumerable<U>, U>, T[])

ZipFold<U>(XElement[], Func<XElement[], IAsyncEnumerable<U>, ValueTask<U>>)

Flatten all of the nodes in the trees represented by values into a single value at the same time, using an aggregation function to combine nodes with the results of aggregating their children. The trees are iterated in lock-step, much like an n-ary Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>).

When trees are not the same size, the larger ones are truncated both horizontally and vertically. That is, if a pair of nodes have a different number of children, the rightmost children of the larger of the two nodes are discarded.

Declaration
public static ValueTask<U> ZipFold<U>(this XElement[] values, Func<XElement[], IAsyncEnumerable<U>, ValueTask<U>> func)
Parameters
Type Name Description

XElement[]

values

The trees to fold

Func<XElement[], IAsyncEnumerable<U>, ValueTask<U>>

func

The aggregation function

Returns
Type Description

ValueTask<U>

The result of aggregating the two trees

Type Parameters
Name Description

U

Remarks

This method is not available on platforms which do not support ValueTask and IAsyncEnumerable<T>.

Examples

Here's an example of using ZipFold<T, U>(IRewriter<T>, Func<T[], IAsyncEnumerable<U>, ValueTask<U>>, T[]) to test if two trees are syntactically equal.

static bool Equals(this Expr left, Expr right)
    => left.ZipFold<Expr, bool>(
        right,
        (xs, results) =>
        {
            switch (xs[0])
            {
                case Add a1 when xs[1] is Add a2:
                    return results.All(x => x);
                case Lit l1 when xs[1] is Lit l2:
                    return l1.Value == l2.Value;
                default:
                    return false;
            }
        }
    );
See Also
ZipFold<T, U>(IRewriter<T>, Func<T[], IAsyncEnumerable<U>, ValueTask<U>>, T[])

ZipFold<U>(XElement[], Func<XElement[], IEnumerable<U>, U>)

Flatten all of the nodes in the trees represented by values into a single value at the same time, using an aggregation function to combine nodes with the results of aggregating their children. The trees are iterated in lock-step, much like an n-ary Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>).

When trees are not the same size, the larger ones are truncated both horizontally and vertically. That is, if a pair of nodes have a different number of children, the rightmost children of the larger of the two nodes are discarded.

Declaration
public static U ZipFold<U>(this XElement[] values, Func<XElement[], IEnumerable<U>, U> func)
Parameters
Type Name Description

XElement[]

values

The trees to fold

Func<XElement[], IEnumerable<U>, U>

func

The aggregation function

Returns
Type Description

U

The result of aggregating the two trees

Type Parameters
Name Description

U

Examples

Here's an example of using ZipFold<T, U>(IRewriter<T>, Func<T[], IEnumerable<U>, U>, T[]) to test if two trees are syntactically equal.

static bool Equals(this Expr left, Expr right)
    => left.ZipFold<Expr, bool>(
        right,
        (xs, results) =>
        {
            switch (xs[0])
            {
                case Add a1 when xs[1] is Add a2:
                    return results.All(x => x);
                case Lit l1 when xs[1] is Lit l2:
                    return l1.Value == l2.Value;
                default:
                    return false;
            }
        }
    );
See Also
ZipFold<T, U>(IRewriter<T>, Func<T[], IEnumerable<U>, U>, T[])