R/loop-transformation.R
can_loop_transform.Rd
This function analyses a recursive function to check if we can transform it into
a loop or trampoline version with transform
. Since this function needs to handle
recursive functions, it needs to know the name of its input function, so this must be
provided as a bare symbol.
can_loop_transform_body(fun_name, fun_body, fun, env) can_loop_transform_(fun) can_loop_transform(fun)
fun_name | Name of the recursive function. |
---|---|
fun_body | The user-transformed function body. |
fun | The function to check. Must be provided by its (bare symbol) name. |
env | Environment used to look up variables used in |
can_loop_transform_body
: This version expects fun_body
to be both tested
and user-transformed.
can_loop_transform_
: This version expects fun
to be quosure.
can_loop_transform
: This version quotes fun
itself.
factorial <- function(n) if (n <= 1) 1 else n * factorial(n - 1) factorial_acc <- function(n, acc = 1) if (n <= 1) acc else factorial_acc(n - 1, n * acc) can_loop_transform(factorial) # FALSE -- and prints a warning#> Warning: The function cannot be transformed since it contains a recursive call inside a call.#> [1] FALSEcan_loop_transform(factorial_acc) # TRUE#> [1] TRUE#> Warning: The function cannot be transformed since it contains a recursive call inside a call.#> [1] FALSE#> [1] TRUE