Like Tree10Likes

Lambdas, why are they so confusing?

This is a discussion on Lambdas, why are they so confusing? within the C++ Programming forums, part of the General Programming Boards category; Pretty much just the topic title. Wow, talk about confusing. It's like the creators of c++11 standard wanted to make ...

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

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    MutantJohn likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    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
    2,432
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    252
    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.
    Elkvis likes this.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,244
    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
    1,090
    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,313
    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%.

  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
    21,781
    I'd say that a fairly common use case would be specifying a predicate when using various standard algorithms.
    grumpy and Elkvis like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    A predicate does return bool, or a value convertible to bool.

Page 1 of 2 12 LastLast
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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21