Overloading Ampersand & operator in R

前端 未结 2 975
小鲜肉
小鲜肉 2021-01-22 10:14

I like to overload my ampersand operator with paste. So that way I can paste stuff easily. Like this:

R> \"Hello\" & \" World\"
 [1] \"Hello World\"
         


        
2条回答
  •  攒了一身酷
    2021-01-22 10:40

    I think defining &.default to use paste is just wrong:

    `&` <- function(e1, e2) UseMethod("&", c(e1, e2))
    `&.default` <- function(e1, e2) .Primitive("&")(e1, e2)
    `&.character` <- function(e1, e2) paste(e1, e2)
    "Hello" & "World"
    [1] "Hello World"
     1*0
    #[1] 0
     1&0
    #[1] FALSE
     1&1
    #[1] TRUE
    

提交回复
热议问题