tidyverse/design

Another compact way of storing enum values

hadley opened this issue · 0 comments

hadley commented

https://substack.com/profile/23309440-brandon-loudermilk

STAT <- list(
  MIN = "min",
  MAX = "max",
  MEAN = "mean"
)

stat <- function(x, stat = STAT$MEAN) {
  if (!stat %in% STAT) {
    supported_stat <- paste0(STAT, collapse = ", ")

    print(glue::glue("Unsupported arg value: stats = '{stat}'; must be one of ({supported_stat})"))
  } else {
    switch(stat,
      mean = mean(x),
      max = max(x),
      min = min(x)
    )
  }
}