STL for_each and generalized iteration

[This article was first published on Rcpp Gallery, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The STL contains a very general looping or sweeping construct in the for_each algorith. It can be used with function objects (such as the simple square function used here) but also with custom class which can be used to keep to keep state.

#include <Rcpp.h>

using namespace Rcpp;

// somewhat silly little class derived from unary_function<T, void> to
// illustrate keeping state -- we interpret the vector x as containing
// growth rates (or returns), and we compute cumulative as well as
// relative gains.
template<class T> class cumProd : public std::unary_function<T, void> {
public:
    cumProd() : cp(1.0), cnt(1) {}      // constructor
    void operator() (T x) {             // default operator()
        cp *= 1.0 + x;
        Rcout << "Iteration "   << cnt++
              << " Growth "     << x
              << " Compounded " << cp 
              << " Proportion " << x/(cp - 1.0)
              << std::endl;
    }
private:  
    double cp;
    int cnt;
};

// [[Rcpp::export]]
void forEach(Rcpp::NumericVector x) {
    std::for_each(x.begin(), x.end(), cumProd<double>());
}

We can illustrate this on a simple example:

set.seed(42)
x <- rnorm(6, 0, 0.01)
x


[1]  0.013710 -0.005647  0.003631  0.006329  0.004043 -0.001061

forEach(x)


Iteration 1 Growth 0.0137096 Compounded 1.01371 Proportion 1
Iteration 2 Growth -0.00564698 Compounded 1.00799 Proportion -0.707182
Iteration 3 Growth 0.00363128 Compounded 1.01165 Proportion 0.31182
Iteration 4 Growth 0.00632863 Compounded 1.01805 Proportion 0.350659
Iteration 5 Growth 0.00404268 Compounded 1.02216 Proportion 0.182403
Iteration 6 Growth -0.00106125 Compounded 1.02108 Proportion -0.0503469

To leave a comment for the author, please follow the link and comment on their blog: Rcpp Gallery.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)