makeStack {lazy.tools} | R Documentation |
Preparation of Stack
makeStack()
This is a function factory which makes the following list of functions.
push, pop, inspect, length
and clear
In the usual image of a stack, the newest object will be placed
on top of the older objects.
However, in this R version, the newest object will be placed
at the end of a hidden list named valueList
in the function factory
environment.
It enables LIFO (Last-In First-Out) or FILO (First-In Last-Out).
A list of functions
push, pop, inspect, length
and clear
The original version is from the answer by Matt:
https://stackoverflow.com/questions/28687806
/a-better-way-to-push-and-pop-to-from-lists-in-r
# generate functions
stack <- makeStack()
stack$push(3)
stack$push(5)
stack$push(9)
stack$length()
stack$inspect(1:stack$length())
stack$pop()
stack$inspect(1:stack$length())
stack$pop()
stack$inspect(1:stack$length())
stack$pop()
# Below causes error.
# stack$inspect(1:stack$length())
STACK=makeStack()
PUSH <- STACK$push
POP <- STACK$pop
LEN <- STACK$length
INSP <- STACK$inspect
CLEAR <- STACK$clear
PUSH( list(a=1,b=2,c="C"))
PUSH( list(a=12,b=22,c="C2"))
PUSH( list(a=13,b=23,c="C3"))
INSP(1:LEN())
POP()
INSP(1:LEN())
POP()
INSP(1:LEN())