Generate safe sequences by strictly enforcing either a positive or negative increment
Details
Creating sequences using either base::seq()
function or :
operator have a notable draw back of incorrectly generating positional
indices when a vector is empty. For example, consider x = NULL
. Then,
the expression 1:length(x)
would evaluate to 1:0
which expands to c(1,0)
.
As a result, any Control
may inadvertantly trigger an out of bounds
error on the initial run.
The safe_seq()
and %:%
operator seeks to prevent this issue by enforcing
either an ascending or descending sequence depending on the by
condition.
For the %:%
operator, note that this is restricted to being always
positive.