Description Value Methods Examples
A simple stack data structure in R, with supporting of assiocated methods, like push, pop and others.
an R6 class object
new()
initialize a new object
simepleStack$new(items = list(), limit = Inf)
items
list, list of items to add to the initial stack
limit
int, how many items can be pushed to the stack, default is unlimited.
len()
returns current length of the stack
simepleStack$len()
get()
returns the full current stack of all items
simepleStack$get()
clear()
remove all items in current stack
simepleStack$clear()
push()
add item(s) to the stack
simepleStack$push(items, after = self$len())
items
list, list of items to add to the stack
after
int, which position to push items after, default is after the current last item. 0 will be before the first item.
pop()
remove item(s) from the stack and return as results
simepleStack$pop(len = 1, tail = FALSE)
len
int, how many items to pop from stack, default is 1 item a time.
tail
bool, to pop in the reverse order (from the last item)? Default
is FALSE
, pop from the top (first item).
clone()
The objects of this class are cloneable with this method.
simepleStack$clone(deep = FALSE)
deep
Whether to make a deep clone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | my_stack <- simepleStack$new()
# check length
my_stack$len()
# add some thing
my_stack$push(list(1, 2, 3))
# print current stack
str(my_stack$get())
# check length
my_stack$len()
# add before the current first
my_stack$push(list(0), after = 0)
# print current stack
str(my_stack$get())
# pop one item
my_stack$pop()
# print current stack
str(my_stack$get())
# pop one item from the tail
my_stack$pop(tail = TRUE)
# print current stack
str(my_stack$get())
# pop more than one items
my_stack$pop(2)
# print current stack
str(my_stack$get()) # nothing left
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.