<story><title>An empirical study on the impact of C++ lambdas and programmer experience</title><url>http://dl.acm.org/authorize?N03390</url></story><parent_chain><item><author>porges</author><text>&amp;gt; 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.&lt;p&gt;These samples do not appear in the paper, so we don&amp;#x27;t know what they saw.&lt;p&gt;The “iterators” discussed are Java-&amp;#x2F;C#-style iterators, not C++ ones (as I expected reading the abstract).&lt;p&gt;In a C++ context I would have expected lambdas vs iterators to be something like:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &amp;#x2F;&amp;#x2F; lambda
    float retVal = 0;
    std::for_each(mb.cbegin(), mb.cend(), [&amp;amp;](item x) { retVal += item.price; });
    return retVal;

    &amp;#x2F;&amp;#x2F; pure iterator
    float retVal = 0;
    for (auto it = mb.cbegin(); it != mb.cend(); ++it)
    {
        retVal += it-&amp;gt;price;
    }

    return retVal;
&lt;/code&gt;&lt;/pre&gt;
... and the first would be better off as:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    return std::accumulate(mb.cbegin(), mb.cend(), 0f,
        [](float acc, item x) { return acc + x.price; });

&lt;/code&gt;&lt;/pre&gt;
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 ;)</text></item></parent_chain><comment><author>MaulingMonkey</author><text>Yeah - from what I can see, neither interface looks like idiomatic C++.&lt;p&gt;EDIT: Looks like you beat me to the punch on some of these ;)&lt;p&gt;Instead of:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  float getSum(marketBasket mb) {
    float retVal = 0;
    &amp;#x2F;&amp;#x2F; Implement solution here
    &amp;#x2F;&amp;#x2F; ---------
    marketBasket::iterator iter = mb.begin();
    while (iter.hasNext()) {
      retVal += iter.get().price;
      iter.next();
    }
    &amp;#x2F;&amp;#x2F; ---------
    return retVal;
  }
&lt;/code&gt;&lt;/pre&gt;
I&amp;#x27;d rather see real SC++L compatible iterators (as hopefully taught) and saner naming:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  float getSum(marketBasket market) {
    float sum = 0;
    &amp;#x2F;&amp;#x2F; Implement solution here
    &amp;#x2F;&amp;#x2F; ---------
    for (marketBasket::iterator item = market.begin(); item != market.end(); ++item) {
      sum += item-&amp;gt;price;
    }
    &amp;#x2F;&amp;#x2F; ---------
    return sum;
  }
&lt;/code&gt;&lt;/pre&gt;
And instead of being pre-provided with a function &amp;lt;void(item)&amp;gt;, if I&amp;#x27;m reading the pdf correctly:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  float getSum(marketBasket mb) {
    float retVal = 0;
    &amp;#x2F;&amp;#x2F; Implement solution here
    &amp;#x2F;&amp;#x2F; ---------
    function &amp;lt;void(item)&amp;gt; func = [&amp;amp;](item theItem) {
      retVal += theItem.price;
    };
    mb.iterateOverItems(func);
    &amp;#x2F;&amp;#x2F; ---------
    return retVal;
  }
&lt;/code&gt;&lt;/pre&gt;
I&amp;#x27;d rather see:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  float getSum(marketBasket market) {
    float sum = 0;
    &amp;#x2F;&amp;#x2F; Implement solution here
    &amp;#x2F;&amp;#x2F; ---------
    market.for_each([&amp;amp;](item theItem) {
      sum += theItem.price;
    });
    &amp;#x2F;&amp;#x2F; ---------
    return sum;
  }
&lt;/code&gt;&lt;/pre&gt;
Or venturing into the far more functional style, where lambdas start to shine for me, personally:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  float getSum(std::vector&amp;lt;item&amp;gt; market) {
    &amp;#x2F;&amp;#x2F; Implement solution here
    &amp;#x2F;&amp;#x2F; ---------
    return std::accumulate(market.begin(), market.end(), 0.0f, [&amp;amp;](float sum, item theItem) {
      return sum + theItem.price;
    });
    &amp;#x2F;&amp;#x2F; ---------
  }
&lt;/code&gt;&lt;/pre&gt;
It looks a bit better in C# where your selection of standard functions is a little less anemic and a little nicer to use:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  float GetSum(MarketBasket market) {
    &amp;#x2F;&amp;#x2F; Implement solution here
    &amp;#x2F;&amp;#x2F; ---------
    return market.Sum(item =&amp;gt; item.Price);
    &amp;#x2F;&amp;#x2F; ---------
  }&lt;/code&gt;&lt;/pre&gt;</text></comment>
<story><title>An empirical study on the impact of C++ lambdas and programmer experience</title><url>http://dl.acm.org/authorize?N03390</url></story><parent_chain><item><author>porges</author><text>&amp;gt; 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.&lt;p&gt;These samples do not appear in the paper, so we don&amp;#x27;t know what they saw.&lt;p&gt;The “iterators” discussed are Java-&amp;#x2F;C#-style iterators, not C++ ones (as I expected reading the abstract).&lt;p&gt;In a C++ context I would have expected lambdas vs iterators to be something like:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &amp;#x2F;&amp;#x2F; lambda
    float retVal = 0;
    std::for_each(mb.cbegin(), mb.cend(), [&amp;amp;](item x) { retVal += item.price; });
    return retVal;

    &amp;#x2F;&amp;#x2F; pure iterator
    float retVal = 0;
    for (auto it = mb.cbegin(); it != mb.cend(); ++it)
    {
        retVal += it-&amp;gt;price;
    }

    return retVal;
&lt;/code&gt;&lt;/pre&gt;
... and the first would be better off as:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    return std::accumulate(mb.cbegin(), mb.cend(), 0f,
        [](float acc, item x) { return acc + x.price; });

&lt;/code&gt;&lt;/pre&gt;
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 ;)</text></item></parent_chain><comment><author>Karliss</author><text>First two tests used lambda with custom for each method. If authors used real c++ iterators and for each loop it would become obvious that comparing iterators and lambdas at iterating is the same as comparing lambdas with if statements at being if statements.</text></comment>