[lambda] Help figuring out variable substitution, lazy functions, etc.? Should I be looking at Phoenix

I have been trying to prevent variable substitution with boost::lambda and cannot figure out how to prevent variable substitution. Here is a (simplified) version: double first_moment(double x) { return x; } //And create a function auto myfunc = first_moment(_1); cout << myfunc(2.0); ... This doesn't compile, and every permutation I can think of using protect, etc. fails as well. Now I realize that this looks like a job for "bind" (which works fine of course) but this is a simplified pattern of the notation I want. Is there any way to do this with protect, unlambda, etc. around the _1? Are other libraries like Phoenix a better place to look for this kind of functionality? Do people see Phoenix as the evolution of boost lambda or are these likely to remain in parallel? Should I stick with C++0X lambdas? Thanks, Jesse

AMDG Jesse Perla wrote:
I have been trying to prevent variable substitution with boost::lambda and cannot figure out how to prevent variable substitution.
Here is a (simplified) version: double first_moment(double x) { return x; }
//And create a function auto myfunc = first_moment(_1); cout << myfunc(2.0);
... This doesn't compile, and every permutation I can think of using protect, etc. fails as well.
Now I realize that this looks like a job for "bind" (which works fine of course) but this is a simplified pattern of the notation I want. Is there any way to do this with protect, unlambda, etc. around the _1?
No. There is no way to make this work by manipulating the _1.
Are other libraries like Phoenix a better place to look for this kind of functionality? Do people see Phoenix as the evolution of boost lambda or are these likely to remain in parallel? Should I stick with C++0X lambdas?
Phoenix allows lazy functions like this struct first_moment_type { typedef double result_type; double operator()(double arg) { return x; } }; phoenix::function<first_moment_type> first_moment; In Christ, Steven Watanabe

On Aug 3, 11:35 am, Steven Watanabe <watanab...@gmail.com> wrote:
AMDG Phoenixallows lazy functions like this
struct first_moment_type { typedef double result_type; double operator()(double arg) { return x; }
};
phoenix::function<first_moment_type> first_moment;
Thanks as always Steven, What is the general thinking on lambda vs. phoenix 2 vs. phoenix 3? Do we expect phoenix 3 to be the main lambda library in boost (eventually)? Should I start getting comfortable with Phoenix 2 to prepare for it or might Phoenix 3 be radically different? Also, I can't figure out in phoenix how I would have lazy operator() on already constructed functors. Is this possible? The main use case of this is to use an expectation operator over an interpolator: -------------------------------------------- template<typename F, typename Distribution> double expectation(F f, Distribution dist){ //Integrates f using dist over its entire support. //... monte carlo, quadrature, etc. } class linear_interpolator{ //construction, etc. double operator()(double x){....} }; //How to make operator() lazy here? I could modify linear interpolator itself if that helps. struct g_type { typedef double result_type; double operator()(double arg) { return x; } }; phoenix::function<first_moment_type> g; //User code linear_interpolator f(....); boost::math::normal_distribution dist(0,1); using namespace boost::phoenix; actor<argument<0> > const _x= argument<0>(); //dummy integration variable cout << expectation( f( _x), dist); //And the main reason I want everything lazy is to allow this which maps //the mathematical models I deal with. cout << expectation( f( g(_x) ), dist); Thanks, -Jesse

Jesse Perla schrieb:
Thanks as always Steven, What is the general thinking on lambda vs. phoenix 2 vs. phoenix 3? Do we expect phoenix 3 to be the main lambda library in boost (eventually)? Should I start getting comfortable with Phoenix 2 to prepare for it or might Phoenix 3 be radically different?
Also, I can't figure out in phoenix how I would have lazy operator() on already constructed functors. Is this possible?
Not sure, what you want. If you want to bind functor this is possible with phoenix. Hre is a code to illustrate this. #include <vector> #include <algorithm> #include <cmath> #include <boost/spirit/include/phoenix_core.hpp> #include <boost/spirit/include/phoenix_function.hpp> using namespace boost::phoenix; using namespace boost::phoenix::arg_names; using namespace std; struct exp__ { template<typename Arg> struct result{typedef Arg type;}; template<typename Arg> Arg operator()(Arg arg) const { return std::exp(arg); } }; struct sin__ { template<typename Arg> struct result{typedef Arg type;}; template<typename Arg> Arg operator()(Arg arg) const { return std::sin(arg); } }; function<sin__> const sin_=sin__(); function<exp__> const exp_=exp__(); int main() { double init[] = { 2, 10, 4, 5, 1, 6, 8, 3, 9, 7 }; vector<double> c(init, init + 10); std::transform(init,init+10,c.begin(), exp_(sin_(arg1)) ); return 0; }
cout << expectation( f( g(_x) ), dist);
Thanks, -Jesse _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Jesse Perla
-
Kim Kuen Tang
-
Steven Watanabe