You may be interested in looking at Reference Classes/objects (see ?setRefClass). This is a form of OO programming that is more similar to C++ and Java. You could create a counter object that you could then increment with syntax like:
x$inc() x$inc(5) The first would increment by the default (1), the second would then increment by 5. On Fri, Oct 17, 2014 at 2:06 AM, PO SU <rhelpmaill...@163.com> wrote: > > Tks for your alternative way's details. but like you mentioned in graphics > package, i still wonder how to overload an operator which can pass one param > like +2 . > There seems exists some examples for my needing. But i try to find them but > without any results. > can you show me some examples from it? > > > > > > > -- > > PO SU > mail: desolato...@163.com > Majored in Statistics from SJTU > > > > At 2014-10-17 15:16:47, "David Winsemius" <dwinsem...@comcast.net> wrote: >> >>On Oct 16, 2014, at 10:36 PM, PO SU wrote: >> >>> >>> Tks for your advice, let the ++ problem alone, how to write an >>> Unary operator ? Is it permitted in R? >>> such as a<-2 , a%+2% will let a be 4 . >> >>OK, that's just wrong. Oh, OK, just for fun, as it were: >> >>inc <- function(x) >>{ >> eval.parent(substitute(x <- x + 1)) >>} >> >> >> > inc(10) >>Error in 10 <- 10 + 1 : invalid (do_set) left-hand side to assignment >> > y=10 >> > inc(y) >> > y >>[1] 11 >> >> >>> I just want to know it , i won't pollute r with it , because i know >>> what is r . : ) >>> >>It's certainly permitted. Just look at all the overloadings of the "+" >>operator in graphics packages. Look up the documentation on methods in >>R. >> >>Why not just use a well-behaved function, though? >> >>.inc <- function(x) x+1 >> > .inc(10) >>[1] 11 >> >>Then you won't be tempted to try 10 <- .inc(10) because it just >>wouldn't make sense. >> >>-- >>David. >> >>> -- >>> >>> PO SU >>> mail: desolato...@163.com >>> Majored in Statistics from SJTU >>> >>> >>> >>> >>> At 2014-10-17 13:09:47, "Rolf Turner" <r.tur...@auckland.ac.nz> wrote: >>>> On 17/10/14 17:29, PO SU wrote: >>>>> >>>>> Dear expeRts, >>>>> Now i want to know how to implement an Unary operator like i++ >>>>> in cpp's synax form. >>>>> e.g. 2++ will let 2 be 3 , a<-2 ,a++ ,will let a be 3 >>>>> I tried this : >>>>> '%++%'<-function(x){ >>>>> x<<-x+1 >>>>> } >>>>> but it have problem, the biggest one is it seems the function need >>>>> twoparams like a%++%b , how to write a function needing just one >>>>> param? >>>>> >>>>> TKS ! >>>> >>>> Just ***DON'T***. The "++" operator is useful only for those wish to >>>> write code which is obscure to the point of incomprehensibility. It >>>> makes C and its offspring "write only" languages. >>>> >>>> If you are going to use R, use R and don't pollute it with such >>>> abominations. >>>> >>>> cheers, >>>> >>>> Rolf Turner >>>> >>>> >>>> -- >>>> Rolf Turner >>>> Technical Editor ANZJS >>> ______________________________________________ >>> R-help@r-project.org mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >>David Winsemius, MD >>Alameda, CA, USA >> > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.