21.12 Unknown Functions

calls_grabber <- function(x) {
  switch_expr(x,
    constant = ,
    symbol =   character(),
    call = {
      fname <- as.character(x[[1]])
      children <- flat_map_chr(as.list(x[-1]), calls_grabber)
      c(fname, children)
    }
  ) |>
    unique()
}

f(g+b,c,d(a))

names_grabber(expr(f(g + b, c, d(a))))
#> [1] "g" "b" "c" "a"
calls_grabber(expr(f(g + b, c, d(a))))
#> [1] "f" "+" "d"
lobstr::ast(expr(f(g + b, c, d(a))))
#> █─expr 
#> └─█─f 
#>   ├─█─`+` 
#>   │ ├─g 
#>   │ └─b 
#>   ├─c 
#>   └─█─d 
#>     └─a

seek_closure <- function(op) {
  # change math font for function names
  # apply ending parenthesis
  new_function(
    exprs(... = ),
    expr({
      contents <- paste(..., collapse = ", ")
      paste0(!!paste0("\\mathrm{", op, "}("), contents, ")")
    })
  )
}