我已经写了堆"类"具有以下功能:add
,push
,pop
,size
,isEmpty
,clear
(多一些).
我想在R中使用这个"类"作为泛型,所以我可以在我的脚本中创建多个堆栈实例.我该怎么做呢?
(我在引号中有类,因为我的堆栈函数是用不同的脚本编写的(不一定是类本身的定义)
提前致谢
list <- ""
cursor = 0
#Initializes stack to empty
stack <- function(){
list <- c()
cursor = -1
assign("list",list,.GlobalEnv)
assign("cursor",cursor,.GlobalEnv)
}
#Where item is a item to be added to generic list
push <- function(item){
if(size(list) == 0){
add(item, -1)
}else{
add(item, 0)
}
assign("list",list,.GlobalEnv)
}
Run Code Online (Sandbox Code Playgroud) 连接命令以生成列表:
line <- cat("list(\"iris\" = iris, \"cars\" = mtcars)")
eval(parse(text = line))
Run Code Online (Sandbox Code Playgroud)
似乎要回归?