R/double_assignment.R

#' Double assignment operator
#'
#' Assigns, similarly to Python, 2 objects to 2 variables
#' @param lhs A pair (2) of variables
#' @param rhs A pair of whatever
#' @keywords assignment, operator
#' @export
#' @examples
#' c(a, b) := c(10, 20)
':=' <- function(lhs, rhs) {
  frame <- parent.frame()
  lhs <- as.list(substitute(lhs))
  if (length(lhs) > 1)
    lhs <- lhs[-1]
  if (length(lhs) == 1) {
    do.call(`=`, list(lhs[[1]], rhs), envir=frame)
    return(invisible(NULL))
  }
  if (is.function(rhs) || is(rhs, 'formula'))
    rhs <- list(rhs)
  if (length(lhs) > length(rhs))
    rhs <- c(rhs, rep(list(NULL), length(lhs) - length(rhs)))
  for (i in 1:length(lhs))
    do.call(`=`, list(lhs[[i]], rhs[[i]]), envir=frame)
  return(invisible(NULL))
}
vuzun/helpers documentation built on May 29, 2019, 11:47 a.m.