I REALLY love lambdas and have the ability to use them in C ++ - it's a pleasure. But since I'm used to Haskell, where lambdas fits very well into the syntax, I try my best to use them in C ++ without writing unreadable cluttered long lines of code.
So, as an example, suppose I would write this:
vector<double> foo(10,0.2); for_each(foo.begin(), foo.end(), [](double x){ std::cout << x << " ";})
It's not that hard to read, the lambda expression is pretty small. But if I have a function with two or three lines inside this for_each, this could be a problem for my code reading skills:
vector<double> foo(10,0.2); randomNumberGenerator bar; for_each(foo.begin(), foo.end(), [](double x){ std::cout << "hello!"; x+=bar()/(1+bar()); std::cout << x << " ";})
This line begins to annoy for a long time and is difficult to read to my taste ...
What are your preferred code conventions for this case? Should I write:
for_each(foo.begin(), foo.end(), [] (double x) { std::cout << "hello!" x += bar()/(1+bar()); std::cout << x << " "; });
or something like that? I still think this syntax seems a bit unnatural and hard to read ... :(
Rafael S. Calsaverini
source share