Boost logo

Boost Users :

Subject: Re: [Boost-users] Two questions on boost::function
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-11-29 21:14:52


On Sun, Nov 29, 2009 at 10:27 AM, CR Koudella <ckoudell_at_[hidden]> wrote:
> Thanks for your reply on this, too. Yes, I am beginning to look into
> phoenix.
> Here too, it would be of great help if you could indicate to me how you
> would
> handle my example with phoenix.

Since you have code in this one I should be able to come up with a
phoenix example:

// Assume this line everywhere:
using namespace boost::phoenix;

// For doing h*(f-g)^2 generically in phoenix:
auto func1 = _3*(_1-_2)*(_1-_2);
// Or more 'optimized' as this (which the compiler may optimize the
above just fine as well, but this demonstrates local vars):
auto func1 = let(_a=(_1-_2))[_3*_a*_a];

// Results in this:
func1(3,2,1) -> 1
func1(6,4,2) -> 8
... etc

That barely demonstrates the surface of phoenix. Phoenix practically
rewrites C++ in C++ in a lazy form, very powerful, but that should
give you an example. As for your nasty binds in binds in binds in
etc... Maybe this?

// Supposing f, g, and h are static functions and there is only one
variable passed in (phoenix cares not what type it is, as long as
f/g/h can resolve it), you were not really clear on exactly what your
code is supposed to do.
auto bfoWRF = let(_a=(bind(&f, _1)-bind(&g, _1)))[bind(&h, _1)*_a*_a]

// In which case if you have this:
std::vector v = {...}
// and you call this:
auto value = bfoWRF(v);
// then it does the same thing as this non-lazy code:
auto a = f(v)-g(v);
auto value = h(v)*a*a;

And yes, you can combine different returns just fine as long as they
obey the normal non-lazy C++ rules.

P.S. If you want to assign bfoWRF to a boost function, it will be as
you may think:
boost::function<double(const std::vector<double> &)> bfoWRF =
let(_a=(bind(&f, _1)-bind(&g, _1)))[bind(&h, _1)*_a*_a]

However, if you leave it as auto (or use BOOST_AUTO for old compiler
support) then it will work on any arbitrary type that would work in
the non-lazy version of the code without requiring that everything be
doubles.

If you clarify your example some more then I can re-demonstrate as well.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net