| > After instructions, participants were given printouts of sample code they could refer to while solving tasks. Group Lambda got code of a C++ program using lambda expressions and group Iterator received code of the same program written using iterators. They then had time to study the samples before starting the tasks and could refer to these samples later. These samples do not appear in the paper, so we don't know what they saw. The “iterators” discussed are Java-/C#-style iterators, not C++ ones (as I expected reading the abstract). In a C++ context I would have expected lambdas vs iterators to be something like: // lambda
float retVal = 0;
std::for_each(mb.cbegin(), mb.cend(), [&](item x) { retVal += item.price; });
return retVal;
// pure iterator
float retVal = 0;
for (auto it = mb.cbegin(); it != mb.cend(); ++it)
{
retVal += it->price;
}
return retVal;
... and the first would be better off as: return std::accumulate(mb.cbegin(), mb.cend(), 0f,
[](float acc, item x) { return acc + x.price; });
I think the need to use ref-capture (since you only get a side-effecting `std::function` to play with in their sample) would be the thing most likely to throw people off – as it’s something that should be avoided in most code, anyway ;) |
EDIT: Looks like you beat me to the punch on some of these ;)
Instead of:
I'd rather see real SC++L compatible iterators (as hopefully taught) and saner naming: And instead of being pre-provided with a function <void(item)>, if I'm reading the pdf correctly: I'd rather see: Or venturing into the far more functional style, where lambdas start to shine for me, personally: It looks a bit better in C# where your selection of standard functions is a little less anemic and a little nicer to use: