Re: [R] how to overwrite a Unary operator ?

2014-10-20 Thread Greg Snow
There is currently no way to write your own unary operator in R.  The
only current unary operators are prefix (-, +, !).  It would take some
major changes to the parser to recognize the syntax that you want
(which could also break other things that already work well), and with
the oo and other methods it is not really needed.  You can technically
pass and empty expression to a binary operator:

> `%++%` <- function(a,b) a + 1
> x <- 5
> x %++% {}
[1] 6

but that does not change the value of the variable (and using `assign`
just creates its own problems).  And syntax like above is more
appropriate for a obfuscated R contest than for anything that you want
to be understood.


There are assignment functions and you could create an object type and
an assignment for such that you might be able to have syntax like:

a <- {}

to increment a, but you would need to declare a class for a before
using it and it would probably be even more complicated than needed.

If you really want C like syntax then you could always make an active binding:

> f <- local({
+   e <- environment()
+   x <- 1
+   list(inc = function(v) {
+ if(missing(v)) {
+  e$x <- e$x + 1
+ } else {
+ stop('unary inc cannot be assigned to')
+ }
+ x
+   },
+   dec = function(v) {
+ e$x <- e$x - 1
+ x
+ } ) } )
>
> makeActiveBinding("x++", f$inc, .GlobalEnv)
> makeActiveBinding("x--", f$dec, .GlobalEnv)
>
> `x++`
[1] 2
> `x++`
[1] 3
> `x++`
[1] 4
> `x--`
[1] 3
> `x--`
[1] 2
> `x++`
[1] 3
>

But it seems simpler to just go with the reference class system at this point.

On Sun, Oct 19, 2014 at 11:17 AM, PO SU  wrote:
>
> It's a good way to use RF OOS, but it's not my needing, actually, i want is 
> there exists a way to write a  %++% form function that can pass one param to 
> it?
> So i can use  1%++%  to get 2 ,a<-2 , a%++% to get a<-3 .
> It seems that the operator overwrite system in R, must pass two params. Is it 
> true?
>
>
>
>
> --
>
> PO SU
> mail: desolato...@163.com
> Majored in Statistics from SJTU
>
>
>
> At 2014-10-18 00:54:40, "Greg Snow" <538...@gmail.com> wrote:
>>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  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"  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?
> suchasa<-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"  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 p

Re: [R] how to overwrite a Unary operator ?

2014-10-19 Thread PO SU

It's a good way to use RF OOS, but it's not my needing, actually, i want is 
there exists a way to write a  %++% form function that can pass one param to it?
So i can use  1%++%  to get 2 ,a<-2 , a%++% to get a<-3 .
It seems that the operator overwrite system in R, must pass two params. Is it 
true?




--

PO SU
mail: desolato...@163.com 
Majored in Statistics from SJTU



At 2014-10-18 00:54:40, "Greg Snow" <538...@gmail.com> wrote:
>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  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"  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?
 suchasa<-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"  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.


Re: [R] how to overwrite a Unary operator ?

2014-10-19 Thread PO SU

TKS , but i still have a question that maybe i can write a new S3 method for +, 
but how can i create a new operator which can pass one param?
becasue + is far away from ++ to let me know that 2 will be 3.
So, it's still a question..




--

PO SU
mail: desolato...@163.com 
Majored in Statistics from SJTU



At 2014-10-18 01:40:50, "David Winsemius"  wrote:
>
>On Oct 17, 2014, at 1:06 AM, PO SU 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? 
>> 
>
>I think this might be a case of "if you don't know enough to do it, then you 
>don't know why you shouldn't do it." (Or vice versa?) I did search for a 
>relevant fortune to support my impression, but the various entries for 
>fortune("parse") and fortune("eval") didn't seem to hit the mark.
>
>library(ggplot2)
>help(pack="ggplot2")  # scroll to bottom of page
>ggplot2:::`+.gg`   # this shows the S3 method of adding an operator based on 
>the S3 method dispatch.
>
>I was able to emulate that example to create a C-like, unary `+` operator for 
>a new class, but I'm not willing to put it in print for fear that my karmic 
>account might be depleted.
>
>-- 
>David.
>
>> --
>> 
>> PO SU
>> mail: desolato...@163.com 
>> Majored in Statistics from SJTU
>> 
>> 
>> 
>> At 2014-10-17 15:16:47, "David Winsemius"  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?
 suchasa<-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"  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
>>> 
>
>David Winsemius
>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.


Re: [R] how to overwrite a Unary operator ?

2014-10-17 Thread David Winsemius

On Oct 17, 2014, at 1:06 AM, PO SU 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? 
> 

I think this might be a case of "if you don't know enough to do it, then you 
don't know why you shouldn't do it." (Or vice versa?) I did search for a 
relevant fortune to support my impression, but the various entries for 
fortune("parse") and fortune("eval") didn't seem to hit the mark.

library(ggplot2)
help(pack="ggplot2")  # scroll to bottom of page
ggplot2:::`+.gg`   # this shows the S3 method of adding an operator based on 
the S3 method dispatch.

I was able to emulate that example to create a C-like, unary `+` operator for a 
new class, but I'm not willing to put it in print for fear that my karmic 
account might be depleted.

-- 
David.

> --
> 
> PO SU
> mail: desolato...@163.com 
> Majored in Statistics from SJTU
> 
> 
> 
> At 2014-10-17 15:16:47, "David Winsemius"  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?
>>> suchasa<-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"  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
>> 

David Winsemius
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.


Re: [R] how to overwrite a Unary operator ?

2014-10-17 Thread Greg Snow
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  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"  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?
>>> suchasa<-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"  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.


Re: [R] how to overwrite a Unary operator ?

2014-10-17 Thread PO SU

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"  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?
>> suchasa<-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"  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.


Re: [R] how to overwrite a Unary operator ?

2014-10-17 Thread Benno Pütz

On 17 Oct 2014, at 06:29, PO SU  wrote:

>   e.g.   2++  will let 2 be 3
That would not even work in C ...

While I use this in C, I second Rolf on the general issue.

Benno__
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.


Re: [R] how to overwrite a Unary operator ?

2014-10-17 Thread David Winsemius


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?

suchasa<-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"  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.


Re: [R] how to overwrite a Unary operator ?

2014-10-16 Thread PO SU

Tks for your advice,  let the ++ problem alone, how to write an Unary operator 
? Is it permitted in R?
suchas    a<-2 , a%+2%  will let a  be 4 .
I just want to know it , i won't pollute r with it , because i know what is r . 
 : )








--

PO SU
mail: desolato...@163.com 
Majored in Statistics from SJTU




At 2014-10-17 13:09:47, "Rolf Turner"  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.


Re: [R] how to overwrite a Unary operator ?

2014-10-16 Thread Rolf Turner

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.


[R] how to overwrite a Unary operator ?

2014-10-16 Thread PO SU

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 two 
params like   a%++%b , how to write a function needing just one param?

TKS !



--

PO SU
mail: desolato...@163.com 
Majored in Statistics from SJTU
__
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.