rgp (version 0.4-1)

MapExpressionNodes: Common higher-order functions for transforming R expressions

Description

MapExpressionNodes transforms an expression expr by replacing every node in the tree with the result of applying a function f. The parameters functions, inners, and leafs control if f should be applied to the function symbols, inner subtrees, and leafs of expr, respectively. MapExpressionLeafs and MapExpressionSubtrees are shorthands for calls to MapExpressionNodes. expr. an expression expr. expr, given as list of nodes and and list of vertices. Each vertex is represented as a pair of indices into the list of nodes. AllExpressionNodes checks if all nodes in the tree of expr satisfy the predicate p (p returns TRUE for every node). This function short-cuts returning FALSE as soon as a node that does not satisfy p is encountered. AnyExpressionNode checks if any node in the tree of expr satisfies the predicate p. This function short-cuts returning TRUE as soon as a node that satisfies p is encountered. subtreeAt returns the subtree at index. replaceSubtreeAt replaces the subtree at index with replacement and returns the result.

Usage

MapExpressionNodes(f, expr, functions = TRUE, inners = FALSE, leafs = TRUE)
MapExpressionLeafs(f, expr)
MapExpressionSubtrees(f, expr)
FlattenExpression(expr)
subtrees(expr, functions = FALSE, inners = TRUE, leafs = TRUE)
expressionGraph(expr)
AllExpressionNodes(p, expr)
AnyExpressionNode(p, expr)
subtreeAt(expr, index)
replaceSubtreeAt(expr, index, replacement)

Arguments

f
The function to apply.
functions
Whether to apply f to the function symbols of expr. Defaults to TRUE.
inners
Whether to apply f to the inner subtrees of expr. Defaults to FALSE.
leafs
Wheter to apply f to the leafs of expr. Defaults to TRUE.
p
The predicate to check.
expr
The expression to transform.
index
An in-order subtree index starting from 0 (the root).
replacement
An expression.

Value

The transformed expression.