Thread: Lambdas, why are they so confusing?

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Lambdas, why are they so confusing?

    Pretty much just the topic title.

    Wow, talk about confusing. It's like the creators of c++11 standard wanted to make it seem convoluted.

    Basically, I think I just got taken back to school.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MutantJohn View Post
    It's like the creators of c++11 standard wanted to make it seem convoluted.
    They just continue the tradition of the language creator.

    Stroutstrup's main goal was to create such complex language, that programmers would always have work supporting code written in it.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The hardest thing about lambdas is how they capture the variables used in the function, which is honestly just something that I look up. I imagine that they were designed to be that complicated because parameters are complicated in C++: you have a number of semantics to choose from, and in other languages, the problem is eliminated by the environment. Other than that, it's like writing a function.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I don't find lambdas difficult at all. apart from the capture clause, their syntax is quite like the rest of C++. it's a new major feature of the language. you can't expect to be fully comfortable with it overnight. just play with them and learn as much as you can. you'll eventually get to the point where it's just as natural to write them as any other part of the language.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by vart View Post
    They just continue the tradition of the language creator.

    Stroutstrup's main goal was to create such complex language, that programmers would always have work supporting code written in it.
    I'm presently trying to wrap my head around Haskell. In hindsight I must say C++ doesn't seem so complicated anymore.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The lambda allows you to write a functor inline, so that the code of the functor is found at the same spot where the functor is used. Before lambdas, functors had to be implemented as classes elsewhere in the source code, making it hard to know what the functor does without scrolling away to another section of code.

    That's really all they're for. You can achieve all the same things a lambda does without using lambda, it's just not as nice.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by brewbuck View Post
    The lambda allows you to write a functor inline, so that the code of the functor is found at the same spot where the functor is used. Before lambdas, functors had to be implemented as classes elsewhere in the source code, making it hard to know what the functor does without scrolling away to another section of code.

    That's really all they're for. You can achieve all the same things a lambda does without using lambda, it's just not as nice.
    Yep, they're especially useful for situations where you need to do some repetitive task within a function.

    Code:
    #include <iostream>
    void wtf( int arg )
    {
        std::cout << arg << " - WTF?!!!" << std::endl;
    }
    int fun( int value )
    {
        int result = 390937;
        auto cleanup = [&result, value]( )
        {
            result = 0;
            wtf( value );
        };    
        if( value < 314821 )
            cleanup( );
        else if( value > 357761 )
            cleanup( );
        else
            result *= value;
        return result;
    }
    int main( )
    {
        fun( 280601 );
    }
    Achieving something like that was always such a pain in the arse with C++03...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Wait Vart, are you serious about that statement? I mean, I laughed but I could almost see it being funny because it's true.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sebastiani View Post
    Achieving something like that was always such a pain in the arse with C++03...
    Your example is not particularly illustrative of anything except poor coding style. This version (without lambda) is much cleaner than your version
    Code:
    int fun( int value )
    {
        int result = 0;
        if ( value < 314821 || value > 357761 )
             wtf(value);
        else
            result = 390937 * value;
        return result;
    }
    Of course, the usage of magic values is poor style, but that's unrelated to your point or mine.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by grumpy
    Your example is not particularly illustrative of anything except poor coding style. This version (without lambda) is much cleaner than your version
    Please try to understand that it was just an abstract example, grumpy. I imagine that must be very hard for your pedantic mind to grasp, but I'm sure that with a little effort even you can appreciate the point of the demonstration.

    Quote Originally Posted by grumpy
    Of course, the usage of magic values is poor style, but that's unrelated to your point or mine.
    Rather, since the numbers are meaningless in the context of the example given, assigning labels to them would only serve to confuse the reader into thinking that the actual values were somehow tied to the point being made. Otherwise I most certainly would have given them concrete names, such as "A001262_26" and so forth...
    Last edited by Sebastiani; 09-28-2013 at 12:01 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'd say that a fairly common use case would be specifying a predicate when using various standard algorithms.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by laserlight View Post
    I'd say that a fairly common use case would be specifying a predicate when using various standard algorithms.
    Predicate? Algorithm? Those big words I not understand.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You are probably kidding, but those terms mean:
    Quote Originally Posted by SirPrattlepod View Post
    Predicate?
    Callback function.
    Algorithm? Those big words I not understand.
    A series of steps to accomplish a goal, like sorting or searching.

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I thought predicate technically meant a boolean function.
    (Or is that just lisp terminology? ..where appending _p after a function signifies that it is a yes/no question.)
    Last edited by manasij7479; 09-28-2013 at 03:44 AM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A predicate does return bool, or a value convertible to bool.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC 4.5 Lambdas
    By Dae in forum C++ Programming
    Replies: 0
    Last Post: 11-23-2009, 03:20 PM
  2. Qt and C++0x lambdas: they don't seem to mix
    By Zach_the_Lizard in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2009, 05:57 AM
  3. Confusing!!!
    By csreddy in forum C Programming
    Replies: 10
    Last Post: 06-26-2007, 07:00 AM
  4. Simple maybe, but confusing at first.
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-17-2006, 11:54 AM
  5. Confusing
    By John in forum C Programming
    Replies: 10
    Last Post: 08-19-2002, 02:01 PM