reduce in Rcpp11

1 min read

Rcpp Rcpp11

And now for something completely different, the reduce function, doing something similar to what the Reduce function does in R:

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
double reduce_example(NumericVector x ){  
    auto add   = [](double a, double b){ return a + b ;} ;
    return reduce(x, add ) ;
}


/*** R
  x <- 1:10
  reduce_example(x)
*/

Giving:

$ Rcpp11Script /tmp/reduce.cpp

> x <- 1:10

> reduce_example(x)
[1] 55