|
Boost Users : |
Subject: Re: [Boost-users] Why use 'functional composition' with boost::bind
From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2010-08-04 07:49:24
Mathias Gaunard wrote:
> Le 03/08/2010 15:35, Wolfram Brenig a écrit :
>> Hi,
>>
>> as a casual/newbie boost-user I'm trying to come to
>> grips with why/when nested functional composition with
>> boost::bind might be of help - following Karlsson's
>> book, in its fifth printing.
>>
>> ----------------------
>> 1) It seems that he suggests, that if one is into
>> doing something like
>>
>> T faa(int);
>> T foo(const& T);
>>
>> int i;
>> vector<T> v(10); // container
>> for(i=0; i<10; i++) {
>> v[i] = faa(i); // set it
>> v[i] = foo(v[i]); // operate on it
>> }
>>
>> then instead of the 'old style' for-loops it would be
>> more desirable to use 'STL style' algorithms with
>> the functors created from boost::bind, say like
>>
>> transform(v.begin(),v.end(),boost::bind(&foo,_1));
>
> You might as well write
> boost::transform(v, foo);
>
> But, personally, since that's a fairly trivial algorithm, I don't find
> that much more interesting than
> foreach(int& i, v)
> i = foo(i);
>
> There are other situations, though, where using higher-order programming
> really shines.
>
>> 2) Then, it is suggested furthermore that if foo()
>> is a (simple?) nested function, say T=double and,
>>
>> double foo(double x) { return 2*x; }
>>
>> then one should do 'functional composition', i.e. create the
>> functor for the previous point 1) by
>>
>> boost::bind(std::multiplies<double>(),2,_1)
>
> Replace all that by just 2*_1
Huh, with which lib? Is _1 in the global namespace, or is this assuming
a using namespace?
Thanks, Jeff
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