Learn R Programming

pmatch (version 0.1.4)

cases_expr_: Create an expression that tests patterns against an expression in turn

Description

Where cases evaluates expressions based on pattern matches, this function creates a long if-else expression that tests patterns in turn and evaluate the expression for a matching pattern. This function is intended for meta-programming rather than usual pattern matching.

Usage

cases_expr_(expr, ...)

cases_expr(expr, ...)

Arguments

expr

The expression to test against. This is usually a bare symbol.

...

Pattern matching rules as in cases.

Functions

  • cases_expr_: Version that expects expr to be quoted.

  • cases_expr: Version that quotes expr itself.

Examples

Run this code
# NOT RUN {
linked_list := NIL | CONS(car, cdr : linked_list)

length_body <- cases_expr(
                  lst,
                  NIL -> acc,
                  CONS(car, cdr) -> ll_length(cdr, acc + 1))
length_body

ll_length <- rlang::new_function(alist(lst=, acc = 0), length_body)
ll_length(CONS(1, CONS(2, CONS(3, CONS(4, NIL)))))

# }

Run the code above in your browser using DataLab