@@ -10,7 +10,6 @@ src: 2012-12-23-sugar-all.cpp
10
10
The sugar function all() answers the question, "Are all of the values ... ?".
11
11
12
12
13
-
14
13
The all_sug() function takes a LogicalVector as an argument and allows one to
15
14
enter an expression for the argument as shown in the R examples. In this example,
16
15
it is simply wrapper around the sugar function all() and includes is_true to
@@ -29,7 +28,6 @@ bool all_sug(LogicalVector x) {
29
28
}
30
29
{% endhighlight %}
31
30
32
-
33
31
{% highlight r %}
34
32
x <- c(3, 9, 0, 2, 7, 5, 6)
35
33
y <- c(0, 0, 0, 0, 0, 0, 0)
@@ -78,7 +76,6 @@ all_sug(y == 0)
78
76
[1] TRUE
79
77
</pre >
80
78
81
-
82
79
While the above function may seem trivial, it can be easy to forget is_true() when
83
80
using all() and will result in a compile error. The check_equal() function below
84
81
is an example of a simple utility function to check two vectors for equality
@@ -87,17 +84,16 @@ using the all_sug() function defined above.
87
84
{% highlight cpp %}
88
85
// [[ Rcpp::export]]
89
86
void check_equal(NumericVector x, NumericVector y) {
90
- if (all_sug(x == y)) {
91
- Rcout << "Success! The input vectors are equal" << std::endl;
92
- // do something
93
- } else {
94
- Rcout << "Fail! The input vectors are not equal" << std::endl;
95
- // do something else
96
- }
87
+ if (all_sug(x == y)) {
88
+ Rcout << "Success! The input vectors are equal" << std::endl;
89
+ // do something
90
+ } else {
91
+ Rcout << "Fail! The input vectors are not equal" << std::endl;
92
+ // do something else
93
+ }
97
94
}
98
95
{% endhighlight %}
99
96
100
-
101
97
{% highlight r %}
102
98
check_equal(x, y)
103
99
{% endhighlight %}
@@ -119,4 +115,3 @@ check_equal(x, c(3, 9, 0, 2, 7, 5, 6))
119
115
<pre class =" output " >
120
116
Success! The input vectors are equal
121
117
</pre >
122
-
0 commit comments