[Rd] order of operations
GILLIBERT, Andre
Andre@G||||bert @end|ng |rom chu-rouen@|r
Sat Aug 28 09:44:06 CEST 2021
For the discussion:
> Yes, and were it not for 0 * NA == NA, you might skip evaluation of y if x evaluates to zero.
With the same idea, NA * (alpha <- 6) could skip the assignment.
I do not think that, on the short term, R would do that thing, but who knows in the future!
As of R 4.1, typeof(`*`) actually returns "builtin" rather than "special", meaning that all its arguments are evaluated.
--
Sincerely
Andre GILLIBERT
________________________________
De : R-devel <r-devel-bounces using r-project.org> de la part de peter dalgaard <pdalgd using gmail.com>
Envoy� : samedi 28 ao�t 2021 09:26:03
� : Duncan Murdoch
Cc : r-devel using r-project.org
Objet : Re: [Rd] order of operations
ATTENTION: Cet e-mail provient d�une adresse mail ext�rieure au CHU de Rouen. Ne cliquez pas sur les liens ou n'ouvrez pas les pi�ces jointes � moins de conna�tre l'exp�diteur et de savoir que le contenu est s�r. En cas de doute, transf�rer le mail � � DSI, S�curit� � pour analyse. Merci de votre vigilance
Yes, and were it not for 0 * NA == NA, you might skip evaluation of y if x evaluates to zero. In Andre Gillibert's example:
1 | (alpha<-6)
there really is no reason to evaluate the assignment since (1 | any) is always TRUE. Notwithstanding method dispatch, that is.
With general function calls, all bets are off. Even f(x <- 1) might decide not to evaluate its argument.
- pd
> On 27 Aug 2021, at 21:14 , Duncan Murdoch <murdoch.duncan using gmail.com> wrote:
>
> On 27/08/2021 3:06 p.m., Enrico Schumann wrote:
>> On Fri, 27 Aug 2021, Gabor Grothendieck writes:
>>> Are there any guarantees of whether x will equal 1 or 2 after this is run?
>>>
>>> (x <- 1) * (x <- 2)
>>> ## [1] 2
>>> x
>>> ## [1] 2
>> At least the "R Language Definition" [1] says
>> "The exponentiation operator �^� and the left
>> assignment plus minus operators �<- - = <<-�
>> group right to left, all other operators group
>> left to right. That is [...] 1 - 1 - 1 is -1"
>> which would imply 2.
>
> I think this is a different issue. There's only one operator in question (the "*"). The question is whether x*y evaluates x first or y first (and I believe the answer is that there are no guarantees). I'm fairly sure both are guaranteed to be evaluated, under the rules for group generics listed in ?groupGeneric, but I'm not certain the guarantee is honoured in all cases.
>
> Duncan Murdoch
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk Priv: PDalgd using gmail.com
______________________________________________
R-devel using r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
[[alternative HTML version deleted]]
More information about the R-devel
mailing list