Making an ordinary function into something useful in lambda expressions

I wish to write some lambda functions to have this sort of behaviour.
#include

On 12/10/2007, Peter Waller
I wish to write some lambda functions to have this sort of behaviour.
#include
#include #include using namespace boost::lambda; using namespace boost;
#include <iostream> using std::cout; using std::endl;
#include <cmath>
int main( int argc, char *argv[] ) { float value; var_type<float>::type theValue( var( value ) );
// Ignore this line #define cos(theta) bind( unlambda(bind( cos, _1 )), theta )
function
myfunction = ( cos( theValue ) - 1 ); value = 2; cout << "The result = " << myfunction(1) << endl; return 0; } Namely, the cos( value ) part.
The code above works, but I feel that using a #define is a little messy, and seems hard to generalise to functions with more arguments. I was wondering if there was a general, better way of turning a given function into one whose execution is delayed.
Thanks in advance,
- Peter
This seems to work:
double value = 3.14159 / 2.0;
boost::function

Close, but not quite what I am after.
I would like to be able to define a function, say, cos, and have it
accept a functor, be it a var_type, and to return another one.
So I could write expressions like result = ( cos( _1 + 2 ) + 2 )(
value ), for example.
It would be nice if I could not have to write bind() where-ever I want
do this, otherwise the expressions will rapidly become unreadable.
TIA
- Peter
On 12/10/2007, Stuart Dootson
On 12/10/2007, Peter Waller
wrote: I wish to write some lambda functions to have this sort of behaviour.
#include
#include #include using namespace boost::lambda; using namespace boost;
#include <iostream> using std::cout; using std::endl;
#include <cmath>
int main( int argc, char *argv[] ) { float value; var_type<float>::type theValue( var( value ) );
// Ignore this line #define cos(theta) bind( unlambda(bind( cos, _1 )), theta )
function
myfunction = ( cos( theValue ) - 1 ); value = 2; cout << "The result = " << myfunction(1) << endl; return 0; } Namely, the cos( value ) part.
The code above works, but I feel that using a #define is a little messy, and seems hard to generalise to functions with more arguments. I was wondering if there was a general, better way of turning a given function into one whose execution is delayed.
Thanks in advance,
- Peter
This seems to work:
double value = 3.14159 / 2.0; boost::function
fn = bind(static_cast (&cos), var(value)); std::cout << fn() << std::endl; value = 0.0; std::cout << fn() << std::endl; At least, the output of 'fn()' changes when 'value' changes, which (as far as I could tell) was what you wanted?
The static_cast is needed to select the appropriate 'cos' overload.
HTH
Stuart Dootson _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Fri, Oct 12, 2007 at 08:30:32PM +0100, Peter Waller wrote:
So I could write expressions like result = ( cos( _1 + 2 ) + 2 )( value ), for example.
You might want to look into Phoenix-2 (part of Spirit).
participants (3)
-
Peter Waller
-
Stuart Dootson
-
Zeljko Vrba