[lamda] smart self binding of a function object

Hi, I usually have function-classes that take two arguments. class f{ double operator()(double x, double y) const { x*y+y; } }; called as: f myf; cout << myf(4.,3.); for some purposes I need a function of only one variable (parametrized in the first or second argument. So I end up doing: using boost::function; using namespace boost::lambda; function<double(double)> myf_at5 = bind(&f::operator(), myf, 5., _1); // myf_at5 is myf(5.,*) So, now I can pass myf_at5 to something that accepts a function of one variable. root_finder( myf_at5, ...); since I have access to f I would like to make f smart enough to handle its own binding. Eg. class f{ ... // same as before function<double(double)> operator()(double x_value, YYYY y_arg){ return bind( &f::operator(), //maybe needs cast to double(*) (double,double) *this, x_value, y_arg ); } function<double(double)> operator()(XXXX x_arg, double y_value){ return bind( &f::operator(), //maybe needs cast *this, x_arg, y_value ); } }; The question is: What should be the type of XXXX and YYYY? in such a way that I can just call root_finder( myf(5., _1) , ... ); or more complicated things like root_finder( myf(5., _1 * 2.), ...); in other words, myf(5.,_1) is the same as the old bind(&f::operator(), myf, 5., _1); Thank you, Alfredo answers using other libraries are also accepted :)

sorry for double posting, the first message had the wrong subject [lamda] instead of [lambda]. On May 11, 3:11 am, alfC <alfredo.cor...@gmail.com> wrote:
Hi,
I usually have function-classes that take two arguments.
class f{ double operator()(double x, double y) const { x*y+y; }
};
called as:
f myf; cout << myf(4.,3.);
for some purposes I need a function of only one variable (parametrized in the first or second argument. So I end up doing:
using boost::function; using namespace boost::lambda; function<double(double)> myf_at5 = bind(&f::operator(), myf, 5., _1); // myf_at5 is myf(5.,*)
So, now I can pass myf_at5 to something that accepts a function of one variable.
root_finder( myf_at5, ...);
since I have access to f I would like to make f smart enough to handle its own binding. Eg.
class f{ ... // same as before function<double(double)> operator()(double x_value, YYYY y_arg){ return bind( &f::operator(), //maybe needs cast to double(*) (double,double) *this, x_value, y_arg ); } function<double(double)> operator()(XXXX x_arg, double y_value){ return bind( &f::operator(), //maybe needs cast *this, x_arg, y_value ); }
};
The question is: What should be the type of XXXX and YYYY?
in such a way that I can just call
root_finder( myf(5., _1) , ... ); or more complicated things like root_finder( myf(5., _1 * 2.), ...);
in other words, myf(5.,_1) is the same as the old bind(&f::operator(), myf, 5., _1);
Thank you, Alfredo answers using other libraries are also accepted :) _______________________________________________ Boost-users mailing list Boost-us...@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Sie haben diese Nachricht erhalten, da Sie der Google Groups-Gruppe Boost Users beigetreten sind. Wenn Sie Nachrichten in dieser Gruppe posten möchten, senden Sie eine E-Mail an boostusers@googlegroups.com. Wenn Sie aus dieser Gruppe austreten möchten, senden Sie eine E-Mail an boostusers+unsubscribe@googlegroups.com. Besuchen Sie die Gruppe unterhttp://groups.google.com/group/boostusers?hl=de, um weitere Optionen zu erhalten.

AMDG alfC wrote:
since I have access to f I would like to make f smart enough to handle its own binding. Eg.
class f{ ... // same as before function<double(double)> operator()(double x_value, YYYY y_arg){ return bind( &f::operator(), //maybe needs cast to double(*) (double,double) *this, x_value, y_arg ); } function<double(double)> operator()(XXXX x_arg, double y_value){ return bind( &f::operator(), //maybe needs cast *this, x_arg, y_value ); } };
The question is: What should be the type of XXXX and YYYY?
You'll have to make operator() a template: template<class T> function<double(double)> operator()(const T& x_arg, double y_arg); In Christ, Steven Watanabe

The question is: What should be the type of XXXX and YYYY?
You'll have to make operator() a template:
template<class T> function<double(double)> operator()(const T& x_arg, double y_arg);
I was about to do that but then I though that doing so would open a can of uncontrolled overload. What if I want to restrict T to lambda expressions? what would be the best way to do it, enable_if? some special tag? (Do lambda expression have any special tag?) Thank you, Alfredo

On 11/05/2010 1:45 PM, alfC wrote:
The question is: What should be the type of XXXX and YYYY?
You'll have to make operator() a template:
template<class T> function<double(double)> operator()(const T& x_arg, double y_arg);
I was about to do that but then I though that doing so would open a can of uncontrolled overload. What if I want to restrict T to lambda expressions? what would be the best way to do it, enable_if? some special tag? (Do lambda expression have any special tag?)
Thank you, Alfredo
What about forcing T to be or be derived from an expression template base class?

You'll have to make operator() a template:
template<class T> function<double(double)> operator()(const T& x_arg, double y_arg);
What if I want to restrict T to lambda expressions?
What about forcing T to be or be derived from an expression template base class?
How can I do that? Can you point me to the right documentation if any. In the context of Boost.Lambda there is no common base class for all expression templates (is there?), for example the placeholders do not inherit from anything. The only thing I found was the subclass 'sig' that seems to be defined for all expression types. But I am still scratching my head on how to expoit this 'sig' subclass. Now that I think one obvious way to restrict T is to be an expression template that 'returns' a double (or convertible to 'double'), this information is contained in the sig subclass. But I am confused now to do any progress. Alfredo

On 11/05/2010 7:47 PM, alfC wrote:
You'll have to make operator() a template:
template<class T> function<double(double)> operator()(const T& x_arg, double y_arg);
What if I want to restrict T to lambda expressions?
What about forcing T to be or be derived from an expression template base class?
How can I do that? Can you point me to the right documentation if any.
In the context of Boost.Lambda there is no common base class for all expression templates (is there?), for example the placeholders do not inherit from anything. The only thing I found was the subclass 'sig' that seems to be defined for all expression types. But I am still scratching my head on how to expoit this 'sig' subclass.
Now that I think one obvious way to restrict T is to be an expression template that 'returns' a double (or convertible to 'double'), this information is contained in the sig subclass. But I am confused now to do any progress.
Alfredo
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Expression-template http://www.boost.org/libs/numeric/ublas/doc/expression_concept.htm C++ Expression Templates By Angelika Langer and Klaus Kreft, March 01, 2003 http://www.drdobbs.com/184401627 I have yet to tinker with expression templates in any meaningful way (aside from copy/pasting the example from wikibooks.org and playing with it), so beyond giving you those links as guides there's not much I can do. I do know that it's exactly what you want though and that the keys to your kingdom are the Var and Constant classes (or equivalent) from the wikibooks.org article. Good luck! Geoff

On 11/05/2010 7:47 PM, alfC wrote:
You'll have to make operator() a template:
template<class T> function<double(double)> operator()(const T& x_arg, double y_arg);
What if I want to restrict T to lambda expressions?
What about forcing T to be or be derived from an expression template base class?
How can I do that? Can you point me to the right documentation if any.
In the context of Boost.Lambda there is no common base class for all expression templates (is there?), for example the placeholders do not inherit from anything. The only thing I found was the subclass 'sig' that seems to be defined for all expression types. But I am still scratching my head on how to expoit this 'sig' subclass.
Now that I think one obvious way to restrict T is to be an expression template that 'returns' a double (or convertible to 'double'), this information is contained in the sig subclass. But I am confused now to do any progress.
Alfredo
I'd also like to add that I'd be very interested to hear about what you've come up with as a solution. Thanks! Geoff

On May 11, 7:10 am, Steven Watanabe <watanab...@gmail.com> wrote:
since I have access to f I would like to make fsmartenough to handle its own binding. Eg. You'll have to make operator() a template: template<class T> function<double(double)> operator()(const T& x_arg, double y_arg);
Thank you Steven for pointing that out. This is simplest still useful solution I found. I realized that returning function<double(double)> would defeat the purpose since it is not a lambda expression an I would need bind again to insert it in a lambda expression this is the soltution class A{ typedef A this_type; double operator()(double const& x) const{return evaluate(x);} //this is the function that performs the actual work //the following creates a lambda expression using operator() template<class LambdaExp> lambda_functor<lambda_functor_base< action<3, function_action<3> >, tuple< double (this_type::* const)(const double&)const, const this_type, //this class lambda_functor<LambdaExp> const > > > operator()(lambda_functor<LambdaExp> const& exp) const{ return bind( static_cast<double(this_type::*)(double const&) const>(&this_type::operator()), static_cast<this_type const&>(*this), //static_cast not necesary but maybe usefull if CRTP worked exp ); } } it can be used as A a; a(5.); //evaluates at 5. a( _1 + _1) or any lambda expression); //gives a bind lambda expression. as you see the most complicated part is the return type of the function. It can be generalized to functions that take two arguments, if mixed symbolic/numeric evaluation is needed other combinations of can be made or simply using lambda::constant for one or two of the arguments. I tried to use CRTP (e.g. class A: public self_binder<A>{...}) to define the new member in a more generic way but I couldn't because the template member function in a base (parent) class can not be resolved by calling A::operator()(lambda_expr). Maybe as OvermindDL1 said Boost.Phoenix can do better in terms of shorted syntax or by allowing CRTP in some way. Unfortunately I don't know enough Phoenix, maybe Phoenix has a "function" class that is a phoenix-lambda by itself. Thank you, Alfredo

On 5/27/10 3:25 PM, alfC wrote:
Maybe as OvermindDL1 said Boost.Phoenix can do better in terms of shorted syntax or by allowing CRTP in some way. Unfortunately I don't know enough Phoenix, maybe Phoenix has a "function" class that is a phoenix-lambda by itself.
Indeed, phoenix does: http://tinyurl.com/35t3yon Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net

On May 27, 12:25 am, alfC <alfredo.cor...@gmail.com> wrote:
This is simplest still useful solution I found. I realized that returning function<double(double)> would defeat the purpose since it is not a lambda expression an I would need bind again to insert it in a lambda expression
this is the soltution (see code in previous post)
this is the generalization to function objects that take *two* arguments, note that there are two template parameters now. As you see the code is still quite long, I didn't find a way to simplify it. (I still didn't manage to use CRTP for example.) class A{ typedef A self_type; ... complex operator()(quantity<atomic::wavenumber> const& k, quantity<atomic::frequency> const& w) const{ //function that does the real work ... } template<class LambdaExp1, class LambdaExp2> lambda_functor<lambda_functor_base< action<4, function_action<4> >, tuple< //signature of function that does the real work complex(self_type::* const)(quantity<atomic::wavenumber> const&, quantity<atomic::frequency> const&) const, const self_type, lambda_functor<LambdaExp1> const, lambda_functor<LambdaExp2> const > > > operator()(lambda_functor<LambdaExp1> const& exp1, lambda_functor<LambdaExp2> const& exp2) const{ return bind( //signature is repeated here static_cast<complex(self_type::* const)(quantity<atomic::wavenumber> const&, quantity<atomic::frequency> const&) const>(&self_type::operator()), static_cast<self_type const&>(*this), //static_cast not necesary until we manage to put this in CRTP base class exp1, exp2); } };

On Tue, May 11, 2010 at 4:11 AM, alfC <alfredo.correa@gmail.com> wrote:
Hi,
I usually have function-classes that take two arguments.
class f{ double operator()(double x, double y) const { x*y+y; } };
called as:
f myf; cout << myf(4.,3.);
for some purposes I need a function of only one variable (parametrized in the first or second argument. So I end up doing:
using boost::function; using namespace boost::lambda; function<double(double)> myf_at5 = bind(&f::operator(), myf, 5., _1); // myf_at5 is myf(5.,*)
So, now I can pass myf_at5 to something that accepts a function of one variable.
root_finder( myf_at5, ...);
since I have access to f I would like to make f smart enough to handle its own binding. Eg.
class f{ ... // same as before function<double(double)> operator()(double x_value, YYYY y_arg){ return bind( &f::operator(), //maybe needs cast to double(*) (double,double) *this, x_value, y_arg ); } function<double(double)> operator()(XXXX x_arg, double y_value){ return bind( &f::operator(), //maybe needs cast *this, x_arg, y_value ); } };
The question is: What should be the type of XXXX and YYYY?
in such a way that I can just call
root_finder( myf(5., _1) , ... ); or more complicated things like root_finder( myf(5., _1 * 2.), ...);
in other words, myf(5.,_1) is the same as the old bind(&f::operator(), myf, 5., _1);
The way I would do this would be Boost.Phoenix, rewrite your f like (I am not making this fully generic, although it would be easy to do, just keeping it as doubles): class f_impl{ template <typename Arg1, typename Arg2> struct result { typedef double type; }; double operator()(double x, double y) const { x*y+y; } }; boost::phoenix::function<f_impl> f; This turns f into a lazy functor, thus for immediate usage you can do: cout << f(4.,3.)(); // Assuming Phoenix3 (in development) with forwarding built-in, so no val's needed. or f_impl myf; cout << myf(4.,3.); But allows lazy generation like: root_finder( f(5., _1) , ... ); or root_finder( f(5., _1 * 2.), ...); If curious, Boost.Phoenix can do what lambda can, but more, it is a generic lazy extension for C++, even something as simple as: val(5) that essentially returns a function of the type int(void), so: cout << val(5)(); return 5 to cout. Obviously a bit of an inconvenience that the immediate form returns a lazy adapter too, but it is the pure lazy way, but allows for a vast amount of power and programming capabilities. (I have even written a few small programming projects completely in Boost.Phoenix to see what it is like, quite fascinating, I do like it, just hits compile time a bit hard when everything is lazy in an entire program, but runs well). Boost.Phoenix2 docs: http://www.boost.org/doc/libs/1_43_0/libs/spirit/phoenix/doc/html/index.html Boost.Phoenix3 is in development, but Phoenix2 is complete, just missing the 'perfect' forwarding adaption.
participants (5)
-
alfC
-
Geoff Hilton
-
Joel de Guzman
-
OvermindDL1
-
Steven Watanabe