Ross Bennett — written Dec 23, 2012 — source
The sugar function all() answers the question, “Are all of the values … ?”.
The all_sug() function takes a LogicalVector as an argument and allows one to
enter an expression for the argument as shown in the R examples. In this example,
it is simply wrapper around the sugar function all() and includes is_true to
return a boolean.
Note that when comparing two vectors, it is an element-wise comparison.
(i.e. x[0] > y[0]
, …, x[n] > y[n]
)
[1] TRUE
[1] FALSE
[1] TRUE
[1] TRUE
While the above function may seem trivial, it can be easy to forget is_true() when using all() and will result in a compile error. The check_equal() function below is an example of a simple utility function to check two vectors for equality using the all_sug() function defined above.
Fail! The input vectors are not equal
Success! The input vectors are equal
tags: sugar
Tweet