R/cases.R
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.
cases_expr_(expr, ...) cases_expr(expr, ...)
expr | The expression to test against. This is usually a bare symbol. |
---|---|
... | Pattern matching rules as in |
cases_expr_
: Version that expects expr
to be quoted.
cases_expr
: Version that quotes expr
itself.
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#> if (!rlang::is_null(..match_env <- pmatch::test_pattern(lst, #> NIL))) with(..match_env, acc) else if (!rlang::is_null(..match_env <- pmatch::test_pattern(lst, #> CONS(car, cdr)))) with(..match_env, ll_length(cdr, acc + #> 1))ll_length <- rlang::new_function(alist(lst=, acc = 0), length_body) ll_length(CONS(1, CONS(2, CONS(3, CONS(4, NIL)))))#> [1] 4