是否有理由更喜欢“&&”超过“&”在'if'语句中,除了短路以外? [英] Is there a reason to prefer '&&' over '&' in 'if' statements, other than short-circuiting?

查看:83
本文介绍了是否有理由更喜欢“&&”超过“&”在'if'语句中,除了短路以外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道,关于以下内容的使用存在很多问题(例如,参见& 与R中的& 相比,但我还没有找到一个能专门回答我的问题的人。 / p>

据我所知,




  • & 像其他算术运算一样,进行元素矢量比较。因此,如果两个参数的长度都大于1,它将返回一个长度大于1的逻辑向量。

  • && 比较第一个两个向量的元素,并且总是返回长度为1的结果。此外,它还会发生短路 cond1&& cond2&& cond3&& ... 仅在 cond1 TRUE <时评估 cond2 / code>,依此类推。这样可以实现 if(exists( is.R)&& is.function(is.R)&& is.R())尤其意味着在某些情况下使用& 是绝对必要的。



此外,如果发出警告


条件的长度> 1且如果其条件具有多个元素,则仅使用第一个元素




从这些初步情况来看,我认为更优选& 而不是& if 语句中的$ c>。



如果有的话在计算过程中出错,我不小心在& 的参数之一中有一个向量,我得到一个警告,这很好。否则,一切都很好。



如果另一方面,我使用了&& ,而我的计算出了点问题,&& 的参数之一是向量,我没有得到警告。这是不好的。如果出于某种原因,我真的想比较两个向量的第一个元素,那么我认为显式而不是隐式地比较容易得多。



注意这违反了R程序员之间似乎普遍达成的协议,并且违反了R docs建议。 (1)



因此,我的问题:除了短路以外,还有什么原因使& 优于 ,如果 c语句是






(1)引用 help(&&)


'&'和'&&'表示逻辑AND,而'|'和'||'表示
逻辑OR。较短的形式在
中执行元素比较,其方式与算术运算符相同。较长的形式
从左到右求值,只检查每个
向量的第一个元素。评估仅进行到确定结果为止。
较长的形式适用于编程控制流,在'if'子句中通常首选



解决方案

简短的回答:是的,不同的符号使读者更清楚地理解含义。



感谢您提出这个有趣的问题!如果我可以总结一下,这似乎是针对您链接的问题的我的答案这一节的后续活动,


...仅当您确定
向量的长度为1时,才想使用长格式。您应该绝对确定向量
仅是长度1,例如在它们是
仅返回长度1布尔值的函数的情况下。如果
向量的长度可能大于1,则要使用缩写形式。因此,如果您不确定,则
应该先检查,或者使用缩写形式,然后使用all和
any将其缩减为长度一,以便在控制流语句中使用,$ b $


我以这种方式听到您的问题(给出评论):但是& & 将在输入长度为1时执行相同的操作,因此除了短路以外,为什么更喜欢& ?也许& 应该是首选,因为如果它们不是长度一个,如果如果会给我一个警告,可以帮助我



首先,我同意@James的评论,即您可能高估了获取值的价值。一个警告;如果不是长度,则更安全的方法是适当地处理此问题,而不仅仅是向前犁。您可以假设& 如果长度不是一个,则应该抛出错误,这也许是一个很好的情况;我不知道为什么会这样做。但是,如果没有时光倒流,我们现在能做的最好的就是检查输入内容是否确实适合您的使用。



然后,您已经检查了如果确定您的输入适当,我仍然会建议&& ,因为它从语义上提醒我,作为读者,我应该确保输入为标量(长度为1)。我习惯于矢量思维,因此此提醒对我有所帮助。它遵循以下原则:不同的运算应具有不同的符号,对我而言,旨在用于标量的运算与矢量化运算具有足够的差异,以至于它需要使用不同的符号。



((不希望发动火焰战争,但这也是为什么我更喜欢<-而不是 = ;一个用于分配变量,一个用于为函数设置参数。虽然这是一回事,但在实践中却足以使不同的符号对我的读者有所帮助。)


Yes I know, there have been a number of questions (see this one, for example) regarding the usage of & vs. && in R, but I have not found one that specifically answers my question.

As I understand the differences,

  • & does element-wise, vectorised comparison, much like the other arithmetic operations. It hence returns a logical vector that has length > 1 if both arguments have length > 1.
  • && compares the first elements of both vectors and always returns a result of length 1. Moreover, it does short-circuiting: cond1 && cond2 && cond3 && ... only evaluates cond2 if cond1 is TRUE, and so forth. This allows for things like if(exists("is.R") && is.function(is.R) && is.R()) and particularly means that using && is strictly necessary in some cases.

Moreover, if issues the warning

the condition has length > 1 and only the first element will be used

if its condition has more than one element.

Judging from these preliminaries, I'd consider it safer to prefer & to && in all if statements where short-circuiting isn't required.

If something went wrong during calculations and I accidentally have a vector in one of &'s arguments, I get a warning, which is good. If not, everything is fine as well.

If, on the other hand, I used &&, and something went wrong in my calculations and one of &&'s arguments is a vector, I don't get a warning. This is bad. If, for some reason, I really want to compare the first elements of two vectors, I'd argue that it's much cleaner to do so explicitly rather than implicitly.

Note that this is contrary to what seems to be common agreement between R programmers and contrary to what the R docs recommend. (1)

Hence my question: Are there any reasons except short-circuiting that make && preferable to & in if statements?


(1) Citing help(&&):

'&' and '&&' indicate logical AND and '|' and '||' indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in 'if' clauses.

解决方案

Short answer: Yes, the different symbol makes the meaning more clear to the reader.

Thanks for this interesting question! If I can summarize, it seems to be a follow-up specifically about this section of my answer to the question you linked,

... you want to use the long forms only when you are certain the vectors are length one. You should be absolutely certain your vectors are only length 1, such as in cases where they are functions that return only length 1 booleans. You want to use the short forms if the vectors are length possibly >1. So if you're not absolutely sure, you should either check first, or use the short form and then use all and any to reduce it to length one for use in control flow statements, like if.

I hear your question (given comments) this way: But & and && will do the same thing if the inputs are length one, so other than short-circuiting, why prefer &&? Perhaps & should be preferred because if they're not length one, if will give me a warning, helping me be even more certain that the inputs are length one.

First, I agree with the comment by @James that you may be "overstating the value of getting a warning"; if it's not length one, the safer thing will be to handle this appropriately, not to just plow ahead. You could make a case that && should throw an error if they're not length one, and perhaps a good case; I don't know the reason why it does what it does. But without going back in time, the best we can do now is to check that the inputs are indeed appropriate for your use.

Given then, that you have checked to make sure your inputs are appropriate, I would still recommend && because it semantically reminds me as the reader that I should be making sure the inputs are scalars (length one). I'm so used to thinking vector-ally that this reminder is helpful to me. It follows the principle that different operations should have different symbols, and for me, a operation that is meant for use on scalars is different enough than a vectorized operation that it warrants a different symbol.

(Not to start a flame war (I hope), but this is also why I prefer <- to =; one for assigning variables, one for setting parameters to functions. Although deep down this is the same thing, it's different enough in practice to make the different symbols helpful to me as a reader.)

这篇关于是否有理由更喜欢“&amp;&amp;”超过“&”在'if'语句中,除了短路以外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆