test_pattern: Test if a pattern matches an expression

Description Usage Arguments Value Functions Examples

View source: R/cases.R

Description

Test if a value, expr, created from constructors matches a pattern of constructors. The test_pattern_ function requires that test_expr is a quoted expression while the test_pattern function expects a bare expression and will quote it itself.

Usage

1
2
3
4
5
test_pattern_(expr, test_expr, eval_env = rlang::caller_env(),
  match_parent_env = rlang::caller_env())

test_pattern(expr, test_expr, eval_env = rlang::caller_env(),
  match_parent_env = rlang::caller_env())

Arguments

expr

A value created using constructors.

test_expr

A constructor pattern to test expr against.

eval_env

The environment where constructors can be found.

match_parent_env

Environment to use as the parent of the match bindings we return. This parameter enables you provide additional values to the environment where match-expressions are evaluated.

Value

NULL if the pattern does not match or an environment with bound variables if it does.

Functions

Examples

1
2
3
4
5
6
7
8
9
type := ZERO | ONE(x) | TWO(x,y)
zero <- ZERO
one <- ONE(1)
two <- TWO(1,2)

as.list(test_pattern(zero, ZERO)) # returns an empty binding
test_pattern_(one, quote(ZERO)) # returns NULL
as.list(test_pattern_(one, quote(ONE(v)))) # returns a binding for v
as.list(test_pattern(two, TWO(v,w))) # returns a binding for v and w

pmatch documentation built on Oct. 19, 2018, 5:04 p.m.