Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-03-08 07:20:19


John Torjo <john.lists_at_[hidden]> writes:

> Aleksey Gurtovoy wrote:
>
>>David Abrahams wrote:
>>
>>
>>>I just tried using the lambda library for the first time. Here's
>>>what came out of it, after some struggle:
>>>
>>> #include <boost/lambda/lambda.hpp>
>>> #include <vector>
>>> #include <numeric>
>>>
>>> using namespace boost::lambda;
>>>
>>> struct X
>>> {
>>> X() : count(1) {}
>>>
>>> unsigned long count;
>>> };
>>>
>>> std::vector<X> xs(10);
>>> unsigned long n = std::accumulate(
>>> xs.begin(), xs.end(), 0UL,
>>> _1 + (&_2) ->* &X::count);
>>>
>>>
>>
>>
>>Views make it much cleaner, both conceptually and syntactically:
>>
>> unsigned long n = accumulate( transform_view( xs,
>> mem_fn(&X::count) )
>> , 0UL
>> );
>>
>>
>
> Indeed ;)
> Using views/ranges makes the code much simpler/clearer.
>
> In time, I've accepted the fact that there's not much point in using
> in-place functions (like _1 + _2, etc.), since in real-life they just
> complicate the code.
> So I just create a new function (in anonymous namespace), and that's it.
>
> And I use boost::bind, which rocks big time :D

What do you suppose boost::bind does other than creating an in-place
function? I believe you guys are confusing issues. While I think
views have benefits, the problems I mentioned above were not caused by
the use of iterators or in-place functions:

  std::vector<X> xs(10);
  unsigned long n = std::accumulate(
      xs.begin(), xs.end(), 0UL,
      _1 + bind(&X::count, _2));
    
Is really no worse than the alternative cited above:

  unsigned long n = accumulate(
      transform_view(
        xs, mem_fn(&X::count) )
    , 0UL);

and the one that uses std::plus instead of "_1 + ..." is really no
fun at all.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk