
David Abrahams wrote:
John Torjo <john.lists@torjo.com> 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
Of course, I knew that ;) I did misexplain however. What I should have said was that I prefer to create a simple and straightforward function (usually a one-liner) and use it with boost::bind over using boost::lambda. Makes the code more self-explanatory.
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);
what we were trying to say was that if you use views/ranges, problems like the one you've shown above usually don't appear/ appear very seldom (at least to me ;) ). Best, John