jonkeane commented on code in PR #13789:
URL: https://github.com/apache/arrow/pull/13789#discussion_r946522488
##########
r/R/dplyr-eval.R:
##########
@@ -121,3 +148,121 @@ format_expr <- function(x) {
}
head(out, 1)
}
+
+#' Translate a function in a given context (environment with bindings)
+#'
+#' Translates a function (if possible) with the help of existing bindings in a
+#' given environment.
+#'
+#' @param fn function to be translated
+#' @param env environment to translate against
+#'
+#' @return a translated function
+#' @keywords internal
+#' @noRd
+translate_to_arrow <- function(.fun, .env) {
+ # get the args and the body of the unknown binding
+ function_formals <- formals(.fun)
+
+ if (is.primitive(.fun)) {
+ # exit as the function can't be translated, we can only translate closures
+ stop("`", as.character(.fun[[1]]), "` is a primitive and cannot be
translated")
+ }
+
+ # TODO handle errors. `fn_body()` errors when fn is a primitive, `body()`
returns
+ # NULL so maybe we can work with that
+ function_body <- rlang::fn_body(.fun)
+
+ if (translatable(.fun, .env)) {
+ translated_function <- rlang::new_function(
+ args = function_formals,
+ body = translate_to_arrow_rec(function_body[[2]])
+ )
+ } else {
+ # TODO WIP if the function is not directly translatable, make one more try
+ # by attempting to translate the unknown calls
+ unknown_function <- setdiff(all_funs(function_body[[2]]), names(.env))
+
+ fn <- as_function(unknown_function, env = caller_env())
+ translated_function <- NULL
+ }
Review Comment:
If I'm reading this code correctly, it looks like it translates two levels
down in the function call, yeah? So if I have only functions in my function
body that have bindings it'll be fine, or if my function calls a function that
also has only functions that have bindings it'll be fine, but if my function
calls a function, which calls another function, which has only bindings it
won't work.
In other words: these will work:
```
my_func <- function(x) {
mean(x)
}
my_func_2 <- function(x) {
my_func(x)
}
```
but this one will not:
```
my_func_3 <- function(x) {
my_func_3(x)
}
```
Or does it actually recurse through the call stack?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]