I got sick of writing null-guard clauses like:

my_cool_function <- function(data, ...) {
  if (is.null(data)) { 
    return(NULL)
  }
  
  #  rest of the function
  nrow(data)
}

So why can’t I just make that a higher order function:

nullsafe_map <- function(x, f, ...) {
  if (is.null(x)) NULL else f(x, ...)
}

mtcars |> 
  nullsafe_map(my_cool_function) |> 
  nullsafe_map(log)
#> [1] 3.465736

NULL |> 
  nullsafe_map(my_cool_function) |> 
  nullsafe_map(log)
#> NULL

It feels like I reinvented something vaguely monad-y here, and I posted the question: “What have I reinvented here?” Tan suggested “purrr::map_if() with .p = Negate(is.null)?” which is cool because I had forgotten about purrr::map_if().

Leave a comment