Created
February 4, 2015 17:22
-
-
Save jjallaire/ded5279b9c552ece9917 to your computer and use it in GitHub Desktop.
boost function and boost bind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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