How to interpret double “or” || and assignment in if clause

浪子不回头ぞ 提交于 2019-12-11 07:36:08

问题


In the function source for stats::bw.nrd0, there is a complicated (to me) if statement:

> bw.nrd0
function (x) 
{
    if (length(x) < 2L) 
        stop("need at least 2 data points")
    hi <- sd(x)
    if (!(lo <- min(hi, IQR(x)/1.34))) 
        (lo <- hi) || (lo <- abs(x[1L])) || (lo <- 1)
    0.9 * lo * length(x)^(-0.2)
}
<bytecode: 0x0000000010c688b0>
<environment: namespace:stats>

Is || to be interpreted in a special way, compared to the regular operator |? Where/how is lo being assigned / re-assigned? How would this be written in "long form"?

Full disclosure, I tried to translate this function to a Python function in this answer, so if you can answer this, you can also add a better answer to that question.


回答1:


Thanks to some helpful comments, I now understand that this means that if lo == 0, we assign to whichever of hi, abs(x[1]) or 1 is non-zero first, in that order. In Python, we could write:

lo = min(hi, iqr/1.34)
lo = lo or hi or abs(x[0]) or 1

or more explicitly:

if not lo:
    if hi:
        lo = hi
    elif abs(x[0]):
        lo = abs(x[0])
    else:
        lo = 1



回答2:


From the help docs:

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



来源:https://stackoverflow.com/questions/38352572/how-to-interpret-double-or-and-assignment-in-if-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!