Skip to content

Instantly share code, notes, and snippets.

@jjallaire
Created February 4, 2015 17:22
Show Gist options
  • Save jjallaire/ded5279b9c552ece9917 to your computer and use it in GitHub Desktop.
Save jjallaire/ded5279b9c552ece9917 to your computer and use it in GitHub Desktop.
boost function and boost bind
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::depends(BH)]]
#include <boost/bind.hpp>
#include <boost/function.hpp>
typedef boost::function<int(int,int)> Combiner;
int add(int x, int y) {
return x + y;
}
int multiply(int x, int y) {
return x * y;
}
int add_then_multiply(int x, int y, int factor) {
return (x + y) * factor;
}
int combine(int x, int y, Combiner combiner) {
return combiner(x, y);
}
// [[Rcpp::export]]
void applyCombiners(int x, int y) {
Rcout << "add: " << combine(x, y, add) << std::endl;
Rcout << "multiply: " << combine(x, y, multiply) << std::endl;
int factor = 8;
Rcout << "add_then_multiply: "
<< combine(x, y, boost::bind(add_then_multiply, _1, _2, factor))
<< std::endl;
}
/*** R
applyCombiners(5, 15)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment