Ross Bennett — written Dec 25, 2012 — source
The fact that expressions with binary logical operators
such as x < 4
create a logical sugar expression
(i.e. a LogicalVector type) is very powerful. This enables
one to easily write simple and expressive functions with
a LogicalVector as an argument.
Any of the logical operators <, <=, >, >=, ==, !=
can be used
to create a logical sugar expression. The Rcpp sugar
vignette has additional examples of using binary logical operators.
[1] 3
[1] 9
A simple function using just C++ and the STL to count the
number of elements in a vector less than a given number could
be written as follows. While this function is simple, the
downside is that additional functions will have to be
written for other logical operators and other types.
Please see the references for the functional and algorithm
headers for information regarding std::less
, bind2nd
, and count_if
.
[1] 3Tweet