Skip to content
Prev 212209 / 398500 Next

Stack type

Here is an example using proto based on converting Duncan's example:

library(proto)
Stack <- proto(new = function(.) proto(Stack,
	stack = NULL,
	push = function(., el) .$stack <- c(list(el), .$stack),
	pop = function(.) { stopifnot(length(.$stack) > 0)
		out <- .$stack[[1]]
		.$stack[[1]] <- NULL
		out
	}))

mystack <- Stack$new()
mystack$push( 1 )
mystack$push( letters )
mystack$pop()
mystack$pop()
mystack$pop() # gives an error
On Mon, Mar 1, 2010 at 8:14 PM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote: