Re: [Rd] can we override "if" in R?

2017-03-06 Thread Gabriel Becker
Mark,

I do understand this desire, though notably, if selecting a single element
from one of these custom vectors gave you a normal value, if would be safe,
right? e.g. if the [ method, etc checked if the index length is one, and
returned the non-compressed value if it was, as an off-the-cuff possibility.

Also, I do feel compelled to say that I'm working with R-core (Luke Tierney
primarily) on low-level support for this kind of data-storage abstraction
that would let you create custom vector/matrix storage mechanisms that
still behave themselves as atomic vectors. See
https://www.r-project.org/dsc/2016/slides/customvectors.html for my initial
pitch and https://svn.r-project.org/R/branches/ALTREP/
<https://svn.r-project.org/R/branches/ALTREP/> for the work-in-progress
branch. We hope to merge that in after the release of 3.4, but there's no
firm date as some work on our part and discussion amongst r-core remains
before it can happen.

So in the future, I hope you'll be able to do this in ways that R sees as
actual atomic vectors. They do have to be define in C-code currently,
though.

~G

On Sun, Mar 5, 2017 at 5:09 PM,  wrote:

> I can't comment for Da, but one example where the ability to make 'if'
> generic would have been desirable:
>
> A couple of years ago I wrote S3 classes and methods for 1-byte integers
> and logicals stored as raw vectors, in order to handle massive amounts of
> genetic data (by the standards of the day). Everything worked pretty
> nicely, ie I could "methodize" just about everything I needed--- except
> if-statements, which would fail to respect eg my definitions of NA. [ The
> precise details elude me, but if() was untrustworthy. ]  To use 'if()',  I
> had to remember to "typecast", which was prone to "user error".
>
> Whether this kind of thing is worth the "risk", is another matter.
>
> cheers
> Mark
>
> Mark Bravington
> CSIRO Marine Lab
> Hobart
> Australia
>
> 
> From: R-devel [r-devel-boun...@r-project.org] on behalf of Gabriel Becker
> [gmbec...@ucdavis.edu]
> Sent: 06 March 2017 11:43
> To: Da Zheng
> Cc: r-devel@r-project.org
> Subject: Re: [Rd] can we override "if" in R?
>
> Da,
>
> I've been following this thread and I'm still confused as to exactly what
> you want/why you want it.
>
> I'm probably just missing some context here, but, If() doesn't operate on
> matrices, generally. Can you give an example of the type of code you want
> to have continue to run that requires if operation *directly* on one of
> your matrix objects, as opposed, say, to a value pulled out from it, or the
> dot-product of two vectors in your system, both of which would be values
> (scalars) not matrices.
>
> Now ifelse(), is of course, a different beast altogether, and would need to
> be overloaded within your system, I imagine.
>
> Best,
> ~G
>
> On Sun, Mar 5, 2017 at 12:52 PM, Da Zheng  wrote:
>
> > On Sun, Mar 5, 2017 at 2:50 PM, Michael Lawrence
> >  wrote:
> > >
> > >
> > > On Sat, Mar 4, 2017 at 12:36 PM, Da Zheng 
> wrote:
> > >>
> > >> In my case, I create a new type of matrices and override matrix
> > >> operations in R for these matrices.
> > >> My goal is to make the system as transparent as possible, which means
> > >> my system should execute the existing R code without modification.
> > >> The problem is that when data is in my own vectors or matrices, "if"
> > >> or "while" can't access their values unless we explicitly convert them
> > >> into R objects. But this means users need to modify the existing code.
> > >> So I hope I can override "if", "while", etc to access data in my own
> > >> vectors and matrices directly.
> > >> Does this sound reasonable?
> > >>
> > >
> > > Would you really need the alternate representation for scalar logicals?
> > >
> > > I can see a case in the deferred evaluation context, although it would
> be
> > > problematic wrt side effects unless the deferral is complete.
> > This is exactly why I want to use my own matrix objects and redefine
> > "if" for the matrices. In my framework, all matrices are read-only, so
> > there isn't side effect.
> >
> > Best,
> > Da
> > >
> > >
> > >
> > >>
> > >> Best,
> > >> Da
> > >>
> > >> On Sat, Mar 4, 2017 at 3:22 PM, Michael Lawrence
> > >>  wrote:
> > >> > I'm curious

Re: [Rd] can we override "if" in R?

2017-03-05 Thread Mark.Bravington
I can't comment for Da, but one example where the ability to make 'if' generic 
would have been desirable:

A couple of years ago I wrote S3 classes and methods for 1-byte integers and 
logicals stored as raw vectors, in order to handle massive amounts of genetic 
data (by the standards of the day). Everything worked pretty nicely, ie I could 
"methodize" just about everything I needed--- except if-statements, which would 
fail to respect eg my definitions of NA. [ The precise details elude me, but 
if() was untrustworthy. ]  To use 'if()',  I had to remember to "typecast", 
which was prone to "user error".

Whether this kind of thing is worth the "risk", is another matter. 

cheers
Mark

Mark Bravington
CSIRO Marine Lab
Hobart
Australia


From: R-devel [r-devel-boun...@r-project.org] on behalf of Gabriel Becker 
[gmbec...@ucdavis.edu]
Sent: 06 March 2017 11:43
To: Da Zheng
Cc: r-devel@r-project.org
Subject: Re: [Rd] can we override "if" in R?

Da,

I've been following this thread and I'm still confused as to exactly what
you want/why you want it.

I'm probably just missing some context here, but, If() doesn't operate on
matrices, generally. Can you give an example of the type of code you want
to have continue to run that requires if operation *directly* on one of
your matrix objects, as opposed, say, to a value pulled out from it, or the
dot-product of two vectors in your system, both of which would be values
(scalars) not matrices.

Now ifelse(), is of course, a different beast altogether, and would need to
be overloaded within your system, I imagine.

Best,
~G

On Sun, Mar 5, 2017 at 12:52 PM, Da Zheng  wrote:

> On Sun, Mar 5, 2017 at 2:50 PM, Michael Lawrence
>  wrote:
> >
> >
> > On Sat, Mar 4, 2017 at 12:36 PM, Da Zheng  wrote:
> >>
> >> In my case, I create a new type of matrices and override matrix
> >> operations in R for these matrices.
> >> My goal is to make the system as transparent as possible, which means
> >> my system should execute the existing R code without modification.
> >> The problem is that when data is in my own vectors or matrices, "if"
> >> or "while" can't access their values unless we explicitly convert them
> >> into R objects. But this means users need to modify the existing code.
> >> So I hope I can override "if", "while", etc to access data in my own
> >> vectors and matrices directly.
> >> Does this sound reasonable?
> >>
> >
> > Would you really need the alternate representation for scalar logicals?
> >
> > I can see a case in the deferred evaluation context, although it would be
> > problematic wrt side effects unless the deferral is complete.
> This is exactly why I want to use my own matrix objects and redefine
> "if" for the matrices. In my framework, all matrices are read-only, so
> there isn't side effect.
>
> Best,
> Da
> >
> >
> >
> >>
> >> Best,
> >> Da
> >>
> >> On Sat, Mar 4, 2017 at 3:22 PM, Michael Lawrence
> >>  wrote:
> >> > I'm curious as to precisely why someone would want to do this.
> >> >
> >> > On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng 
> wrote:
> >> >>
> >> >> I'm just curious. Why making "if" generic is even more dangerous?
> >> >>
> >> >> Best,
> >> >> Da
> >> >>
> >> >> On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi  >
> >> >> wrote:
> >> >> > `!` is a generic, `if` is not. You can define an `if` that is
> >> >> > generic,
> >> >> > but this might be even more dangerous
> >> >> >
> >> >> > ❯ `if` <- function(a, b, c) UseMethod("if")
> >> >> > ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
> >> >> > ❯ `if.foo` <- function(a, b, c) FALSE
> >> >> > ❯ a <- structure(42, class = "foo")
> >> >> >
> >> >> > ❯ if (a) TRUE else FALSE
> >> >> > [1] FALSE
> >> >> >
> >> >> > ❯ if (1) TRUE else FALSE
> >> >> > [1] TRUE
> >> >> >
> >> >> > Gabor
> >> >> >
> >> >> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng 
> >> >> > wrote:
> >> >> >> Thanks.
> >> >> >> Can I override it for a specific class?
> >> >> &

Re: [Rd] can we override "if" in R?

2017-03-05 Thread William Dunlap via R-devel
Da Zheng would like to override 'if' and 'while' to accept more than
scalar logicals and Martin Maechler would like to change 'if' to
accept only scalar logicals.  No one has mentioned '||' and '&&',
which also want scalar logicals.

Perhaps a solution is to have all of these call a new generic
function, as.scalar.logical() on their relevant arguments.  (For
efficiency, the default would be internally dispatched).

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sun, Mar 5, 2017 at 4:43 PM, Gabriel Becker  wrote:
> Da,
>
> I've been following this thread and I'm still confused as to exactly what
> you want/why you want it.
>
> I'm probably just missing some context here, but, If() doesn't operate on
> matrices, generally. Can you give an example of the type of code you want
> to have continue to run that requires if operation *directly* on one of
> your matrix objects, as opposed, say, to a value pulled out from it, or the
> dot-product of two vectors in your system, both of which would be values
> (scalars) not matrices.
>
> Now ifelse(), is of course, a different beast altogether, and would need to
> be overloaded within your system, I imagine.
>
> Best,
> ~G
>
> On Sun, Mar 5, 2017 at 12:52 PM, Da Zheng  wrote:
>
>> On Sun, Mar 5, 2017 at 2:50 PM, Michael Lawrence
>>  wrote:
>> >
>> >
>> > On Sat, Mar 4, 2017 at 12:36 PM, Da Zheng  wrote:
>> >>
>> >> In my case, I create a new type of matrices and override matrix
>> >> operations in R for these matrices.
>> >> My goal is to make the system as transparent as possible, which means
>> >> my system should execute the existing R code without modification.
>> >> The problem is that when data is in my own vectors or matrices, "if"
>> >> or "while" can't access their values unless we explicitly convert them
>> >> into R objects. But this means users need to modify the existing code.
>> >> So I hope I can override "if", "while", etc to access data in my own
>> >> vectors and matrices directly.
>> >> Does this sound reasonable?
>> >>
>> >
>> > Would you really need the alternate representation for scalar logicals?
>> >
>> > I can see a case in the deferred evaluation context, although it would be
>> > problematic wrt side effects unless the deferral is complete.
>> This is exactly why I want to use my own matrix objects and redefine
>> "if" for the matrices. In my framework, all matrices are read-only, so
>> there isn't side effect.
>>
>> Best,
>> Da
>> >
>> >
>> >
>> >>
>> >> Best,
>> >> Da
>> >>
>> >> On Sat, Mar 4, 2017 at 3:22 PM, Michael Lawrence
>> >>  wrote:
>> >> > I'm curious as to precisely why someone would want to do this.
>> >> >
>> >> > On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng 
>> wrote:
>> >> >>
>> >> >> I'm just curious. Why making "if" generic is even more dangerous?
>> >> >>
>> >> >> Best,
>> >> >> Da
>> >> >>
>> >> >> On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi > >
>> >> >> wrote:
>> >> >> > `!` is a generic, `if` is not. You can define an `if` that is
>> >> >> > generic,
>> >> >> > but this might be even more dangerous
>> >> >> >
>> >> >> > ❯ `if` <- function(a, b, c) UseMethod("if")
>> >> >> > ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
>> >> >> > ❯ `if.foo` <- function(a, b, c) FALSE
>> >> >> > ❯ a <- structure(42, class = "foo")
>> >> >> >
>> >> >> > ❯ if (a) TRUE else FALSE
>> >> >> > [1] FALSE
>> >> >> >
>> >> >> > ❯ if (1) TRUE else FALSE
>> >> >> > [1] TRUE
>> >> >> >
>> >> >> > Gabor
>> >> >> >
>> >> >> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng 
>> >> >> > wrote:
>> >> >> >> Thanks.
>> >> >> >> Can I override it for a specific class?
>> >> >> >> I can do that for operators such as "!". For example, "!.fm" works
>> >> >> >> for
>> >> >> >> objects of the class "fm".
>> >> >> >> It seems I can't do the same for "if".
>> >> >> >>
>> >> >> >> Best,
>> >> >> >> Da
>> >> >> >>
>> >> >> >> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi
>> >> >> >> 
>> >> >> >> wrote:
>> >> >> >>> You can. Perhaps needless to say, be careful with this.
>> >> >> >>>
>> >> >> >>> ❯ `if` <- function(...) FALSE
>> >> >> >>> ❯ if (TRUE) TRUE else FALSE
>> >> >> >>> [1] FALSE
>> >> >> >>>
>> >> >> >>> G.
>> >> >> >>>
>> >> >> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng 
>> >> >> >>> wrote:
>> >> >>  Hello,
>> >> >> 
>> >> >>  I heard we can override almost everything in R. Is it possible
>> to
>> >> >>  override "if" keyword in R to evaluate my own object instead of
>> a
>> >> >>  logical value?
>> >> >> 
>> >> >>  Thanks,
>> >> >>  Da
>> >> >> 
>> >> >>  __
>> >> >>  R-devel@r-project.org mailing list
>> >> >>  https://stat.ethz.ch/mailman/listinfo/r-devel
>> >> >>
>> >> >> __
>> >> >> R-devel@r-project.org mailing list
>> >> >> https://stat.ethz.ch/mailman/listinfo/r-devel
>> >> >
>> >> >
>> >
>> >
>>
>> __
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/

Re: [Rd] can we override "if" in R?

2017-03-05 Thread Gabriel Becker
Da,

I've been following this thread and I'm still confused as to exactly what
you want/why you want it.

I'm probably just missing some context here, but, If() doesn't operate on
matrices, generally. Can you give an example of the type of code you want
to have continue to run that requires if operation *directly* on one of
your matrix objects, as opposed, say, to a value pulled out from it, or the
dot-product of two vectors in your system, both of which would be values
(scalars) not matrices.

Now ifelse(), is of course, a different beast altogether, and would need to
be overloaded within your system, I imagine.

Best,
~G

On Sun, Mar 5, 2017 at 12:52 PM, Da Zheng  wrote:

> On Sun, Mar 5, 2017 at 2:50 PM, Michael Lawrence
>  wrote:
> >
> >
> > On Sat, Mar 4, 2017 at 12:36 PM, Da Zheng  wrote:
> >>
> >> In my case, I create a new type of matrices and override matrix
> >> operations in R for these matrices.
> >> My goal is to make the system as transparent as possible, which means
> >> my system should execute the existing R code without modification.
> >> The problem is that when data is in my own vectors or matrices, "if"
> >> or "while" can't access their values unless we explicitly convert them
> >> into R objects. But this means users need to modify the existing code.
> >> So I hope I can override "if", "while", etc to access data in my own
> >> vectors and matrices directly.
> >> Does this sound reasonable?
> >>
> >
> > Would you really need the alternate representation for scalar logicals?
> >
> > I can see a case in the deferred evaluation context, although it would be
> > problematic wrt side effects unless the deferral is complete.
> This is exactly why I want to use my own matrix objects and redefine
> "if" for the matrices. In my framework, all matrices are read-only, so
> there isn't side effect.
>
> Best,
> Da
> >
> >
> >
> >>
> >> Best,
> >> Da
> >>
> >> On Sat, Mar 4, 2017 at 3:22 PM, Michael Lawrence
> >>  wrote:
> >> > I'm curious as to precisely why someone would want to do this.
> >> >
> >> > On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng 
> wrote:
> >> >>
> >> >> I'm just curious. Why making "if" generic is even more dangerous?
> >> >>
> >> >> Best,
> >> >> Da
> >> >>
> >> >> On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi  >
> >> >> wrote:
> >> >> > `!` is a generic, `if` is not. You can define an `if` that is
> >> >> > generic,
> >> >> > but this might be even more dangerous
> >> >> >
> >> >> > ❯ `if` <- function(a, b, c) UseMethod("if")
> >> >> > ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
> >> >> > ❯ `if.foo` <- function(a, b, c) FALSE
> >> >> > ❯ a <- structure(42, class = "foo")
> >> >> >
> >> >> > ❯ if (a) TRUE else FALSE
> >> >> > [1] FALSE
> >> >> >
> >> >> > ❯ if (1) TRUE else FALSE
> >> >> > [1] TRUE
> >> >> >
> >> >> > Gabor
> >> >> >
> >> >> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng 
> >> >> > wrote:
> >> >> >> Thanks.
> >> >> >> Can I override it for a specific class?
> >> >> >> I can do that for operators such as "!". For example, "!.fm" works
> >> >> >> for
> >> >> >> objects of the class "fm".
> >> >> >> It seems I can't do the same for "if".
> >> >> >>
> >> >> >> Best,
> >> >> >> Da
> >> >> >>
> >> >> >> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi
> >> >> >> 
> >> >> >> wrote:
> >> >> >>> You can. Perhaps needless to say, be careful with this.
> >> >> >>>
> >> >> >>> ❯ `if` <- function(...) FALSE
> >> >> >>> ❯ if (TRUE) TRUE else FALSE
> >> >> >>> [1] FALSE
> >> >> >>>
> >> >> >>> G.
> >> >> >>>
> >> >> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng 
> >> >> >>> wrote:
> >> >>  Hello,
> >> >> 
> >> >>  I heard we can override almost everything in R. Is it possible
> to
> >> >>  override "if" keyword in R to evaluate my own object instead of
> a
> >> >>  logical value?
> >> >> 
> >> >>  Thanks,
> >> >>  Da
> >> >> 
> >> >>  __
> >> >>  R-devel@r-project.org mailing list
> >> >>  https://stat.ethz.ch/mailman/listinfo/r-devel
> >> >>
> >> >> __
> >> >> R-devel@r-project.org mailing list
> >> >> https://stat.ethz.ch/mailman/listinfo/r-devel
> >> >
> >> >
> >
> >
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Gabriel Becker, PhD
Associate Scientist (Bioinformatics)
Genentech Research

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-05 Thread Da Zheng
On Sun, Mar 5, 2017 at 2:50 PM, Michael Lawrence
 wrote:
>
>
> On Sat, Mar 4, 2017 at 12:36 PM, Da Zheng  wrote:
>>
>> In my case, I create a new type of matrices and override matrix
>> operations in R for these matrices.
>> My goal is to make the system as transparent as possible, which means
>> my system should execute the existing R code without modification.
>> The problem is that when data is in my own vectors or matrices, "if"
>> or "while" can't access their values unless we explicitly convert them
>> into R objects. But this means users need to modify the existing code.
>> So I hope I can override "if", "while", etc to access data in my own
>> vectors and matrices directly.
>> Does this sound reasonable?
>>
>
> Would you really need the alternate representation for scalar logicals?
>
> I can see a case in the deferred evaluation context, although it would be
> problematic wrt side effects unless the deferral is complete.
This is exactly why I want to use my own matrix objects and redefine
"if" for the matrices. In my framework, all matrices are read-only, so
there isn't side effect.

Best,
Da
>
>
>
>>
>> Best,
>> Da
>>
>> On Sat, Mar 4, 2017 at 3:22 PM, Michael Lawrence
>>  wrote:
>> > I'm curious as to precisely why someone would want to do this.
>> >
>> > On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng  wrote:
>> >>
>> >> I'm just curious. Why making "if" generic is even more dangerous?
>> >>
>> >> Best,
>> >> Da
>> >>
>> >> On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi 
>> >> wrote:
>> >> > `!` is a generic, `if` is not. You can define an `if` that is
>> >> > generic,
>> >> > but this might be even more dangerous
>> >> >
>> >> > ❯ `if` <- function(a, b, c) UseMethod("if")
>> >> > ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
>> >> > ❯ `if.foo` <- function(a, b, c) FALSE
>> >> > ❯ a <- structure(42, class = "foo")
>> >> >
>> >> > ❯ if (a) TRUE else FALSE
>> >> > [1] FALSE
>> >> >
>> >> > ❯ if (1) TRUE else FALSE
>> >> > [1] TRUE
>> >> >
>> >> > Gabor
>> >> >
>> >> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng 
>> >> > wrote:
>> >> >> Thanks.
>> >> >> Can I override it for a specific class?
>> >> >> I can do that for operators such as "!". For example, "!.fm" works
>> >> >> for
>> >> >> objects of the class "fm".
>> >> >> It seems I can't do the same for "if".
>> >> >>
>> >> >> Best,
>> >> >> Da
>> >> >>
>> >> >> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi
>> >> >> 
>> >> >> wrote:
>> >> >>> You can. Perhaps needless to say, be careful with this.
>> >> >>>
>> >> >>> ❯ `if` <- function(...) FALSE
>> >> >>> ❯ if (TRUE) TRUE else FALSE
>> >> >>> [1] FALSE
>> >> >>>
>> >> >>> G.
>> >> >>>
>> >> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng 
>> >> >>> wrote:
>> >>  Hello,
>> >> 
>> >>  I heard we can override almost everything in R. Is it possible to
>> >>  override "if" keyword in R to evaluate my own object instead of a
>> >>  logical value?
>> >> 
>> >>  Thanks,
>> >>  Da
>> >> 
>> >>  __
>> >>  R-devel@r-project.org mailing list
>> >>  https://stat.ethz.ch/mailman/listinfo/r-devel
>> >>
>> >> __
>> >> R-devel@r-project.org mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/r-devel
>> >
>> >
>
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-05 Thread Michael Lawrence
On Sat, Mar 4, 2017 at 12:36 PM, Da Zheng  wrote:

> In my case, I create a new type of matrices and override matrix
> operations in R for these matrices.
> My goal is to make the system as transparent as possible, which means
> my system should execute the existing R code without modification.
> The problem is that when data is in my own vectors or matrices, "if"
> or "while" can't access their values unless we explicitly convert them
> into R objects. But this means users need to modify the existing code.
> So I hope I can override "if", "while", etc to access data in my own
> vectors and matrices directly.
> Does this sound reasonable?
>
>
Would you really need the alternate representation for scalar logicals?

I can see a case in the deferred evaluation context, although it would be
problematic wrt side effects unless the deferral is complete.




> Best,
> Da
>
> On Sat, Mar 4, 2017 at 3:22 PM, Michael Lawrence
>  wrote:
> > I'm curious as to precisely why someone would want to do this.
> >
> > On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng  wrote:
> >>
> >> I'm just curious. Why making "if" generic is even more dangerous?
> >>
> >> Best,
> >> Da
> >>
> >> On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi 
> >> wrote:
> >> > `!` is a generic, `if` is not. You can define an `if` that is generic,
> >> > but this might be even more dangerous
> >> >
> >> > ❯ `if` <- function(a, b, c) UseMethod("if")
> >> > ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
> >> > ❯ `if.foo` <- function(a, b, c) FALSE
> >> > ❯ a <- structure(42, class = "foo")
> >> >
> >> > ❯ if (a) TRUE else FALSE
> >> > [1] FALSE
> >> >
> >> > ❯ if (1) TRUE else FALSE
> >> > [1] TRUE
> >> >
> >> > Gabor
> >> >
> >> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng 
> wrote:
> >> >> Thanks.
> >> >> Can I override it for a specific class?
> >> >> I can do that for operators such as "!". For example, "!.fm" works
> for
> >> >> objects of the class "fm".
> >> >> It seems I can't do the same for "if".
> >> >>
> >> >> Best,
> >> >> Da
> >> >>
> >> >> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi <
> csardi.ga...@gmail.com>
> >> >> wrote:
> >> >>> You can. Perhaps needless to say, be careful with this.
> >> >>>
> >> >>> ❯ `if` <- function(...) FALSE
> >> >>> ❯ if (TRUE) TRUE else FALSE
> >> >>> [1] FALSE
> >> >>>
> >> >>> G.
> >> >>>
> >> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng 
> >> >>> wrote:
> >>  Hello,
> >> 
> >>  I heard we can override almost everything in R. Is it possible to
> >>  override "if" keyword in R to evaluate my own object instead of a
> >>  logical value?
> >> 
> >>  Thanks,
> >>  Da
> >> 
> >>  __
> >>  R-devel@r-project.org mailing list
> >>  https://stat.ethz.ch/mailman/listinfo/r-devel
> >>
> >> __
> >> R-devel@r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> >
>

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-05 Thread Hadley Wickham
On Sun, Mar 5, 2017 at 8:13 PM, Gábor Csárdi  wrote:
> Because the S3 class system is very informal. E.g. if you happen to
> have an `if.whatever` function, that will be automatically a method of
> your generic.

For example:

x <- structure(1:10, class = "test")
t(x)
#>
#>  One Sample t-test
#>
#> data:  x
#> t = 5.7446, df = 9, p-value = 0.0002782
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#>  3.334149 7.665851
#> sample estimates:
#> mean of x
#>   5.5


-- 
http://hadley.nz

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-04 Thread Gábor Csárdi
Because the S3 class system is very informal. E.g. if you happen to
have an `if.whatever` function, that will be automatically a method of
your generic.

Gabor

On Sat, Mar 4, 2017 at 7:49 PM, Da Zheng  wrote:
> I'm just curious. Why making "if" generic is even more dangerous?
>
> Best,
> Da
>
> On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi  wrote:
>> `!` is a generic, `if` is not. You can define an `if` that is generic,
>> but this might be even more dangerous
>>
>> ❯ `if` <- function(a, b, c) UseMethod("if")
>> ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
>> ❯ `if.foo` <- function(a, b, c) FALSE
>> ❯ a <- structure(42, class = "foo")
>>
>> ❯ if (a) TRUE else FALSE
>> [1] FALSE
>>
>> ❯ if (1) TRUE else FALSE
>> [1] TRUE
>>
>> Gabor
>>
>> On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng  wrote:
>>> Thanks.
>>> Can I override it for a specific class?
>>> I can do that for operators such as "!". For example, "!.fm" works for
>>> objects of the class "fm".
>>> It seems I can't do the same for "if".
>>>
>>> Best,
>>> Da
>>>
>>> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi  
>>> wrote:
 You can. Perhaps needless to say, be careful with this.

 ❯ `if` <- function(...) FALSE
 ❯ if (TRUE) TRUE else FALSE
 [1] FALSE

 G.

 On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng  wrote:
> Hello,
>
> I heard we can override almost everything in R. Is it possible to
> override "if" keyword in R to evaluate my own object instead of a
> logical value?
>
> Thanks,
> Da
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-04 Thread William Dunlap via R-devel
dplyr::translate_sql() redefines lots of functions, include "if", to
translate from R syntax to SQL syntax.

> dplyr::translate_sql(if ("mpg">25) "better" else "worse")
 CASE WHEN ('mpg' > 25.0) THEN ('better') ELSE ('worse') END

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sat, Mar 4, 2017 at 12:22 PM, Michael Lawrence
 wrote:
> I'm curious as to precisely why someone would want to do this.
>
> On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng  wrote:
>
>> I'm just curious. Why making "if" generic is even more dangerous?
>>
>> Best,
>> Da
>>
>> On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi 
>> wrote:
>> > `!` is a generic, `if` is not. You can define an `if` that is generic,
>> > but this might be even more dangerous
>> >
>> > ❯ `if` <- function(a, b, c) UseMethod("if")
>> > ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
>> > ❯ `if.foo` <- function(a, b, c) FALSE
>> > ❯ a <- structure(42, class = "foo")
>> >
>> > ❯ if (a) TRUE else FALSE
>> > [1] FALSE
>> >
>> > ❯ if (1) TRUE else FALSE
>> > [1] TRUE
>> >
>> > Gabor
>> >
>> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng  wrote:
>> >> Thanks.
>> >> Can I override it for a specific class?
>> >> I can do that for operators such as "!". For example, "!.fm" works for
>> >> objects of the class "fm".
>> >> It seems I can't do the same for "if".
>> >>
>> >> Best,
>> >> Da
>> >>
>> >> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi 
>> wrote:
>> >>> You can. Perhaps needless to say, be careful with this.
>> >>>
>> >>> ❯ `if` <- function(...) FALSE
>> >>> ❯ if (TRUE) TRUE else FALSE
>> >>> [1] FALSE
>> >>>
>> >>> G.
>> >>>
>> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng 
>> wrote:
>>  Hello,
>> 
>>  I heard we can override almost everything in R. Is it possible to
>>  override "if" keyword in R to evaluate my own object instead of a
>>  logical value?
>> 
>>  Thanks,
>>  Da
>> 
>>  __
>>  R-devel@r-project.org mailing list
>>  https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>> __
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-04 Thread Da Zheng
In my case, I create a new type of matrices and override matrix
operations in R for these matrices.
My goal is to make the system as transparent as possible, which means
my system should execute the existing R code without modification.
The problem is that when data is in my own vectors or matrices, "if"
or "while" can't access their values unless we explicitly convert them
into R objects. But this means users need to modify the existing code.
So I hope I can override "if", "while", etc to access data in my own
vectors and matrices directly.
Does this sound reasonable?

Best,
Da

On Sat, Mar 4, 2017 at 3:22 PM, Michael Lawrence
 wrote:
> I'm curious as to precisely why someone would want to do this.
>
> On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng  wrote:
>>
>> I'm just curious. Why making "if" generic is even more dangerous?
>>
>> Best,
>> Da
>>
>> On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi 
>> wrote:
>> > `!` is a generic, `if` is not. You can define an `if` that is generic,
>> > but this might be even more dangerous
>> >
>> > ❯ `if` <- function(a, b, c) UseMethod("if")
>> > ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
>> > ❯ `if.foo` <- function(a, b, c) FALSE
>> > ❯ a <- structure(42, class = "foo")
>> >
>> > ❯ if (a) TRUE else FALSE
>> > [1] FALSE
>> >
>> > ❯ if (1) TRUE else FALSE
>> > [1] TRUE
>> >
>> > Gabor
>> >
>> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng  wrote:
>> >> Thanks.
>> >> Can I override it for a specific class?
>> >> I can do that for operators such as "!". For example, "!.fm" works for
>> >> objects of the class "fm".
>> >> It seems I can't do the same for "if".
>> >>
>> >> Best,
>> >> Da
>> >>
>> >> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi 
>> >> wrote:
>> >>> You can. Perhaps needless to say, be careful with this.
>> >>>
>> >>> ❯ `if` <- function(...) FALSE
>> >>> ❯ if (TRUE) TRUE else FALSE
>> >>> [1] FALSE
>> >>>
>> >>> G.
>> >>>
>> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng 
>> >>> wrote:
>>  Hello,
>> 
>>  I heard we can override almost everything in R. Is it possible to
>>  override "if" keyword in R to evaluate my own object instead of a
>>  logical value?
>> 
>>  Thanks,
>>  Da
>> 
>>  __
>>  R-devel@r-project.org mailing list
>>  https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>> __
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-04 Thread Michael Lawrence
I'm curious as to precisely why someone would want to do this.

On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng  wrote:

> I'm just curious. Why making "if" generic is even more dangerous?
>
> Best,
> Da
>
> On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi 
> wrote:
> > `!` is a generic, `if` is not. You can define an `if` that is generic,
> > but this might be even more dangerous
> >
> > ❯ `if` <- function(a, b, c) UseMethod("if")
> > ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
> > ❯ `if.foo` <- function(a, b, c) FALSE
> > ❯ a <- structure(42, class = "foo")
> >
> > ❯ if (a) TRUE else FALSE
> > [1] FALSE
> >
> > ❯ if (1) TRUE else FALSE
> > [1] TRUE
> >
> > Gabor
> >
> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng  wrote:
> >> Thanks.
> >> Can I override it for a specific class?
> >> I can do that for operators such as "!". For example, "!.fm" works for
> >> objects of the class "fm".
> >> It seems I can't do the same for "if".
> >>
> >> Best,
> >> Da
> >>
> >> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi 
> wrote:
> >>> You can. Perhaps needless to say, be careful with this.
> >>>
> >>> ❯ `if` <- function(...) FALSE
> >>> ❯ if (TRUE) TRUE else FALSE
> >>> [1] FALSE
> >>>
> >>> G.
> >>>
> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng 
> wrote:
>  Hello,
> 
>  I heard we can override almost everything in R. Is it possible to
>  override "if" keyword in R to evaluate my own object instead of a
>  logical value?
> 
>  Thanks,
>  Da
> 
>  __
>  R-devel@r-project.org mailing list
>  https://stat.ethz.ch/mailman/listinfo/r-devel
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-04 Thread Da Zheng
I'm just curious. Why making "if" generic is even more dangerous?

Best,
Da

On Sat, Mar 4, 2017 at 1:22 PM, Gábor Csárdi  wrote:
> `!` is a generic, `if` is not. You can define an `if` that is generic,
> but this might be even more dangerous
>
> ❯ `if` <- function(a, b, c) UseMethod("if")
> ❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
> ❯ `if.foo` <- function(a, b, c) FALSE
> ❯ a <- structure(42, class = "foo")
>
> ❯ if (a) TRUE else FALSE
> [1] FALSE
>
> ❯ if (1) TRUE else FALSE
> [1] TRUE
>
> Gabor
>
> On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng  wrote:
>> Thanks.
>> Can I override it for a specific class?
>> I can do that for operators such as "!". For example, "!.fm" works for
>> objects of the class "fm".
>> It seems I can't do the same for "if".
>>
>> Best,
>> Da
>>
>> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi  wrote:
>>> You can. Perhaps needless to say, be careful with this.
>>>
>>> ❯ `if` <- function(...) FALSE
>>> ❯ if (TRUE) TRUE else FALSE
>>> [1] FALSE
>>>
>>> G.
>>>
>>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng  wrote:
 Hello,

 I heard we can override almost everything in R. Is it possible to
 override "if" keyword in R to evaluate my own object instead of a
 logical value?

 Thanks,
 Da

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-04 Thread Gábor Csárdi
`!` is a generic, `if` is not. You can define an `if` that is generic,
but this might be even more dangerous

❯ `if` <- function(a, b, c) UseMethod("if")
❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
❯ `if.foo` <- function(a, b, c) FALSE
❯ a <- structure(42, class = "foo")

❯ if (a) TRUE else FALSE
[1] FALSE

❯ if (1) TRUE else FALSE
[1] TRUE

Gabor

On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng  wrote:
> Thanks.
> Can I override it for a specific class?
> I can do that for operators such as "!". For example, "!.fm" works for
> objects of the class "fm".
> It seems I can't do the same for "if".
>
> Best,
> Da
>
> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi  wrote:
>> You can. Perhaps needless to say, be careful with this.
>>
>> ❯ `if` <- function(...) FALSE
>> ❯ if (TRUE) TRUE else FALSE
>> [1] FALSE
>>
>> G.
>>
>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng  wrote:
>>> Hello,
>>>
>>> I heard we can override almost everything in R. Is it possible to
>>> override "if" keyword in R to evaluate my own object instead of a
>>> logical value?
>>>
>>> Thanks,
>>> Da
>>>
>>> __
>>> R-devel@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-04 Thread Da Zheng
Thanks.
Can I override it for a specific class?
I can do that for operators such as "!". For example, "!.fm" works for
objects of the class "fm".
It seems I can't do the same for "if".

Best,
Da

On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi  wrote:
> You can. Perhaps needless to say, be careful with this.
>
> ❯ `if` <- function(...) FALSE
> ❯ if (TRUE) TRUE else FALSE
> [1] FALSE
>
> G.
>
> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng  wrote:
>> Hello,
>>
>> I heard we can override almost everything in R. Is it possible to
>> override "if" keyword in R to evaluate my own object instead of a
>> logical value?
>>
>> Thanks,
>> Da
>>
>> __
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] can we override "if" in R?

2017-03-04 Thread Gábor Csárdi
You can. Perhaps needless to say, be careful with this.

❯ `if` <- function(...) FALSE
❯ if (TRUE) TRUE else FALSE
[1] FALSE

G.

On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng  wrote:
> Hello,
>
> I heard we can override almost everything in R. Is it possible to
> override "if" keyword in R to evaluate my own object instead of a
> logical value?
>
> Thanks,
> Da
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

[Rd] can we override "if" in R?

2017-03-04 Thread Da Zheng
Hello,

I heard we can override almost everything in R. Is it possible to
override "if" keyword in R to evaluate my own object instead of a
logical value?

Thanks,
Da

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel