R/stack.R

Defines functions new_stack

new_stack <- function() {

  .data  <- list()
  .index <- 0

  push <- function(data) {
    .index <<- .index + 1
    .data[[.index]] <<- data
  }

  pop <- function() {
    if (.index == 0)
      return(NULL)

    result <- .data[[.index]]
    .index <<- .index - 1
    result
  }

  peek <- function() {
    if (.index == 0)
      return(NULL)
    .data[[.index]]
  }

  list(push = push, pop = pop, peek = peek)
}

Try the rmarkdown package in your browser

Any scripts or data that you put into this service are public.

rmarkdown documentation built on Sept. 18, 2023, 5:17 p.m.