天道酬勤,学无止境

generic-function

Is 'show' a normal S4 generic function?

I'm trying to create a method for my class, which inherits from data.frame. I was originally hoping just to inherit the 'show' method from data.frame as well, but I'm also fine with writing my own. I defined my class and 'show' method as follows: setClass("SCvec", representation(auth = "character", dev = "character", sensor = "character", channel = "character", starttime = "character", endtime = "character"), contains="data.frame") setMethod("show", signature(x="SCvec"), function(x) print(x)) when I type show in the R console, it prints out: standardGeneric for "show" defined from package

2022-03-15 17:39:54    分类:问答    r   show   s4   generic-function

Common Lisp: Generic Function Specializing on Array Length

I am just getting started with generic functions and am wondering if this is possible (I really hope so!). I have made 3 packages for handling vectors of different lengths: vector2, vector3 and vector4. Each package has functions that handle vectors of that length: vector2:normalize - for normalizing *vector2s* vector3:normalize - for normalizing *vector3s* etc. My vectors are typed arrays (for speed and memory use as this is for writing games) so a vector3 is: (make-array 3 :element-type `single-float). Now I am writing a package called vectors which will contain generic functions to handle

2022-02-10 05:39:05    分类:问答    types   common-lisp   generic-function

Replacing an ordinary function with a generic function

I'd like to use names such as elt, nth and mapcar with a new data structure that I am prototyping, but these names designate ordinary functions and so, I think, would need to be redefined as generic functions. Presumably it's bad form to redefine these names? Is there a way to tell defgeneric not to generate a program error and to go ahead and replace the function binding? Is there a good reason for these not being generic functions or is just historic? What's the considered wisdom and best practice here please?

2021-08-01 05:20:00    分类:问答    common-lisp   clos   generic-function

R:从列表对象创建自定义输出(R: Create custom output from list object)

问题 我有一个存储不同数据类型和对象的列表: header <- "This is a header." a <- 10 b <- 20 c <- 30 w <- 1:10 x <- 21:30 y <- 51:60 z <- 0:9 mylist <- list(header = header, const = list(a = a, b = b, c = c), data = data.frame(w,x,y,z)) 现在我希望 R 以下列格式显示这个列表: This is a header. Values: a: 10 b: 20 c: 30 Data: w x y z 1 1 21 51 0 2 2 22 52 1 3 3 23 53 2 4 4 24 54 3 5 5 25 55 4 6 6 26 56 5 7 7 27 57 6 8 8 28 58 7 9 9 29 59 8 10 10 30 60 9 有没有方便的方法来做到这一点? 回答1 如果您想定期使用这种print ,我将使用如下class : class(mylist) <- "myclass" print.myclass <- function(x, ...){ cat(x$header,"\n\n") cat("Values: ", sprintf("%s: %s", names(x$const), x

2021-06-22 17:02:51    分类:技术分享    r   generic-function

R: Create custom output from list object

I have a list that stores different data types and objects: header <- "This is a header." a <- 10 b <- 20 c <- 30 w <- 1:10 x <- 21:30 y <- 51:60 z <- 0:9 mylist <- list(header = header, const = list(a = a, b = b, c = c), data = data.frame(w,x,y,z)) Now I want R to display this list in the following format: This is a header. Values: a: 10 b: 20 c: 30 Data: w x y z 1 1 21 51 0 2 2 22 52 1 3 3 23 53 2 4 4 24 54 3 5 5 25 55 4 6 6 26 56 5 7 7 27 57 6 8 8 28 58 7 9 9 29 59 8 10 10 30 60 9 Is there a convenient way to do this?

2021-05-11 01:35:45    分类:问答    r   generic-function