[Rd] Strict seq: a recommendation

Brahm, David David.Brahm at geodecapital.com
Wed Aug 23 18:30:34 CEST 2006


I use:
  for (m in beg %:% end) {...}
where
  "%:%" <- function(a, b) if (b >= a) a:b

-- David Brahm (brahm at alum.mit.edu) 



-----Original Message-----
From: r-devel-bounces at r-project.org
[mailto:r-devel-bounces at r-project.org] On Behalf Of Dominick Samperi
Sent: Wednesday, August 23, 2006 11:13 AM
To: r-devel at r-project.org
Subject: [Rd] Strict seq: a recommendation

In for loops of the form:
for(m in seq(beg,end))
...

when beg > end, the default behavior is
to decrement, and the action described
by ... is executed at least once.

On the other hand, if you view this
construction as a translation of the C code:
for(m = beg; m < end; m++)
...

then the semantics of C/C++ is not
respected, because the code in ... is
not executed when beg > end in
the case of C/C++.

To get the proper C/C++ semantics I
use the following replacement for seq:

seqStrict <- function(beg, end, step=1) {
  if((beg >= end && step > 0) || (beg <= end && step < 0))
    val <- c() # null vector
  else
    val <- seq(beg,end,step)
  val
}

Perhaps it would be useful to add an option to
seq, so seq(beg,end,strict=TRUE) amounts to
the same thing as using seqStrict(beg,end).

ds

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



More information about the R-devel mailing list