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
Support my work on patreon Blogging is one of the activities I have the freedom to do because of community sponsorship. If you like the content, would like to see more, or just generally like my work, please consider pledging.