Variadic Functions in R: Forward Extra Arguments With ... to Inner Calls

Quick Overview

Implement three R functions that accept extra arguments through ... and forward them to summary statistics, a named list of user functions, and grepl. It tests how R forwards dots, which callees reject unknown arguments, and how to inspect ... before passing it on.

Variadic Functions in R: Forward Extra Arguments With ... to Inner Calls

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement three variadic functions in `/app/solution.R` that capture extra arguments through `...` and forward them to inner functions. - `summary_stats(x, ...)`: return a named numeric vector of length 4 with names in this exact order, `c("mean", "median", "sd", "var")`, computed as `mean(x, ...)`, `median(x, ...)`, `sd(x, ...)` and `var(x, ...)`. - `apply_funs(x, funs, ...)`: `funs` is a named list of functions. Return a named numeric vector (names taken from `funs`) whose element i is `funs[[i]](x, ...)`. - `count_matches(x, pattern, ...)`: return a numeric scalar, the number of elements of the character vector `x` that match the regex `pattern`, forwarding `...` (for example `ignore.case = TRUE` or `fixed = TRUE`) to `grepl`. Example: ```r summary_stats(c(1, 2, 3, NA), na.rm = TRUE) #> mean median sd var #> 2 2 1 1 apply_funs(c(1, 2, 3), list(min = min, max = max)) #> min max #> 1 3 count_matches(c("Apple", "banana", "apricot"), "^a", ignore.case = TRUE) #> [1] 2 ``` ```hint Not every callee has dots Compare `args(mean)` with `args(sd)` and `args(grepl)` before you decide what can safely be forwarded to all of them. ``` ### Constraints & Assumptions - In `summary_stats` and `apply_funs`, `x` is a numeric vector. In `count_matches`, `x` is a character vector and may contain `NA`. - Every function in `funs` returns a single number when called on `x`. - Extra arguments are passed by name (`na.rm = TRUE`, `ignore.case = TRUE`). ### Clarifying Questions to Ask - Should `summary_stats` accept options that only some of the four statistics understand, such as `trim`? - What should `apply_funs` do if one of the functions returns more than one value or a non-numeric value? - Does "numeric scalar" for `count_matches` allow an integer, or must it be a double? ### What a Strong Answer Covers - Forwards `...` unchanged into each inner call and returns the names in the required order. - Builds the `apply_funs` result so the names come from `funs` and each result is checked to be one number. - Counts matches with a logical vector and states how `NA` elements of `x` are counted. - Explains which callees silently accept unknown arguments and which raise an "unused argument" error. - Points out the risk of unnamed arguments in `...` matching positionally in the inner calls. ### Follow-up Questions - What happens if a forwarded argument isn't accepted by every inner function? - How would you capture `...` as a list to inspect or filter it before forwarding? - Why can the anonymous function inside `apply_funs` use the outer `...`, and what changes if you pass `...` through `vapply` instead?

Overview: Implement three R functions that accept extra arguments through ... and forward them to summary statistics, a named list of user functions, and grepl. It tests how R forwards dots, which callees reject unknown arguments, and how to inspect ... before passing it on.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement three variadic functions in /app/solution.R that capture extra arguments through ... and forward them to inner functions.

  • summary_stats(x, ...) : return a named numeric vector of length 4 with names in this exact order, c("mean", "median", "sd", "var") , computed as mean(x, ...) , median(x, ...) , sd(x, ...) and var(x, ...) .
  • apply_funs(x, funs, ...) : funs is a named list of functions. Return a named numeric vector (names taken from funs ) whose element i is funs[[i]](x, ...) .
  • count_matches(x, pattern, ...) : return a numeric scalar, the number of elements of the character vector x that match the regex pattern , forwarding ... (for example ignore.case = TRUE or fixed = TRUE ) to grepl .

Example:

summary_stats(c(1, 2, 3, NA), na.rm = TRUE)
#>   mean median     sd    var
#>      2      2      1      1

apply_funs(c(1, 2, 3), list(min = min, max = max))
#> min max
#>   1   3

count_matches(c("Apple", "banana", "apricot"), "^a", ignore.case = TRUE)
#> [1] 2

Constraints & Assumptions

  • In summary_stats and apply_funs , x is a numeric vector. In count_matches , x is a character vector and may contain NA .
  • Every function in funs returns a single number when called on x .
  • Extra arguments are passed by name ( na.rm = TRUE , ignore.case = TRUE ).

Clarifying Questions to Ask Guidance

  • Should summary_stats accept options that only some of the four statistics understand, such as trim ?
  • What should apply_funs do if one of the functions returns more than one value or a non-numeric value?
  • Does "numeric scalar" for count_matches allow an integer, or must it be a double?

What a Strong Answer Covers Guidance

  • Forwards ... unchanged into each inner call and returns the names in the required order.
  • Builds the apply_funs result so the names come from funs and each result is checked to be one number.
  • Counts matches with a logical vector and states how NA elements of x are counted.
  • Explains which callees silently accept unknown arguments and which raise an "unused argument" error.
  • Points out the risk of unnamed arguments in ... matching positionally in the inner calls.

Follow-up Questions Guidance

  • What happens if a forwarded argument isn't accepted by every inner function?
  • How would you capture ... as a list to inspect or filter it before forwarding?
  • Why can the anonymous function inside apply_funs use the outer ... , and what changes if you pass ... through vapply instead?