C++ Weekly - Ep 426 - Lambdas As State Machines

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024

Комментарии • 50

  • @kethernet
    @kethernet 5 месяцев назад +25

    Minor nitpick: under "case: Startup", "state = Numbers" should be outside the conditional. As it stands, "3-3" and "33-" parse as -33.

  • @MrModez
    @MrModez 5 месяцев назад +16

    6:10 the modification didn't compile in time for the video section, made me chuckle 😂 Ah C++, I love you as much as I hate you..

  • @Offdopp
    @Offdopp 5 месяцев назад +18

    Haskell devs are giving us the thousand yard stare right now

    • @alskidan
      @alskidan 5 месяцев назад

      Not lazy enough 😂

  • @anon_y_mousse
    @anon_y_mousse 5 месяцев назад +3

    Interesting. Now I'm wondering what a BF interpreter would look like. Imagine using #embed to import your program and run it at compile time. ++++++++++[>+++++++>+.

  • @ignacionr
    @ignacionr 5 месяцев назад +11

    hmmm that CLion ad

  • @YarikTH
    @YarikTH 5 месяцев назад +10

    It is literally a parser class without any flexibility that class gives you

    • @turtlesoupage
      @turtlesoupage 5 месяцев назад +6

      It's an example to illustrate the idea.

    • @YarikTH
      @YarikTH 5 месяцев назад +7

      @@turtlesoupage and I don't see any advantage this idea has over writing a classic C++ class

    • @AtomicAndi
      @AtomicAndi 5 месяцев назад +4

      Yeah, I definitely missed a hint if/when this makes sense or if it is just toying around

    • @turtlesoupage
      @turtlesoupage 5 месяцев назад +3

      ​@@YarikTH It's useful when you need a simple state machine confined to a single method/function. There's no need for the cognitive overhead and context switching of creating a separate class somewhere else to refer to. Of course you could declare a state machine class inside the function, but that would still be more verbose.

    • @NathanielNiles
      @NathanielNiles 5 месяцев назад +1

      @@turtlesoupage I'm already spending effort ignoring the generated classes that hold my lambdas, and this is really just "programming the compiler" to put more stuff I can't see into the generated class, which I find to be more of a cognitive burden than defining the class explicitly.
      That said, I think this would have been a good lightning talk, based on the recently released CPPCon '23 videos.

  • @davidesleeds
    @davidesleeds 5 месяцев назад +2

    You finally convinced me to buy the lambda llama shirt!

  • @eggmeister6641
    @eggmeister6641 5 месяцев назад +5

    Two porn bots in the comments.

    • @nyyakko
      @nyyakko 5 месяцев назад

      I always report those, they're so annoying.

    • @headlibrarian1996
      @headlibrarian1996 5 месяцев назад

      @@nyyakko That doesn’t seem to do much to stop the spammers. Tons of financial adviser spam in many videos I watch. They’re doing several posts per video, presumably in the hope one of them doesn’t get reported.

    • @cppweekly
      @cppweekly  5 месяцев назад

      Sorry - I do make an effort to remove those. I don't check for them daily though, so any efforts by viewers to report them is appreciated!

    • @eggmeister6641
      @eggmeister6641 5 месяцев назад

      @@cppweekly It wasn't intended as a criticism of you, in case that's how it came across.

  • @KX36
    @KX36 5 месяцев назад +1

    i was just speculating in my head the other day about implementing coroutines through lambdas just like this.

    • @cppweekly
      @cppweekly  4 месяца назад +1

      github.com/lefticus/tools/blob/main/include/lefticus/tools/lambda_coroutines.hpp

  • @sirhenrystalwart8303
    @sirhenrystalwart8303 5 месяцев назад

    At this point, just declaring an inline struct seems to make it more obvious that you want to maintain state across calls, and the reader of you code doesn't have to remember nearly as much c++ trivia. E.g., just now I had to go google what "mutable" means in this context. Skill issue? Sure. But with c++, nearly everybody has skill issues.

  • @Byynx
    @Byynx 5 месяцев назад

    Do you have a video about "language binding FFI" and what really happens under the hood? I know since almost everything is written in C++/C maybe it doesn't makes sense to talk about it but would still be great to understand that logic, for better understanding of computer logic.

  • @N....
    @N.... 5 месяцев назад

    I found that the ability for lambdas to capture variadic template parameters is quite convenient and ends up making you want to turn them into state machines rather than making a separate class that captures and uses the variadic stuff in a different way. Feels like a bit of an antipattern though maybe...

  • @neozoan
    @neozoan 5 месяцев назад

    Reminds me a bit of generators from Python (sorta).

  • @Stasenko58
    @Stasenko58 5 месяцев назад

    With all due respect I don't get the purpose of this video. As a occasional teacher myself I would never try to explain lambda as anything but a syntactic sugar over a class. And I don't think it should ever be explained in a different way. It acts like a class, compiles to a class, it is a class (and classes often do have states, which is quite obvious). Trying to push lambda as something other than a class, a separate concept only messes with people new to c++. Firstly in makes it harder to understand, because instead of just saying it is a concept that is known (or should be known to people who learn lambdas), and here is how lambda syntax translates to a class, it forces to learn a new concept from scratch and maybe overtime student will see that it acts in a same way. Secondly it adds confusion of what lambda actually is. What are the magic capture variables, or this magic mutable keyword? When to use lambda instead of function or class? Those questions may seem trivial to someone who has deep knowledge of c++, but are more then likely to be asked by the targeted audience of such video.

    • @not_ever
      @not_ever 5 месяцев назад

      That's great for you as a teacher of C++. Is Jason teaching C++ or making interesting videos about C++ and all the interesting and weird things that it can do?
      "I thought this was an interesting aside for this channel, and as you know, I love to talk about lambdas." Sounds like the latter to me.
      I'm certainly not here to learn C++ from Jason, I here because I like seeing things I probably wouldn't want to do in a million years but it's cool to know they exist and if I learn useful things along the way then I see that as a bonus.

    • @bloodgain
      @bloodgain 5 месяцев назад +1

      Thinking about lambdas as "magic" classes is also misleading, IMO, even if it's how they are implemented. Rather than teach that they are syntactic sugar for classes, you should be teaching about closures. Classes are a special case of closures, though that idea may be a bit advanced in an entry-level C++ class. It's more of a theory and design concept. But closures are an important concept that has implications in many languages, especially with all the adoption of functional syntax in modern language. Some languages that support closures don't have the concept of classes at all. The concept of closures is portable to every language that uses lambdas, which is most of them now.
      That said, this channel is not only about entry-level C++ concepts. Quite the contrary, in fact. While he has some more entry-level videos, and a new series all about fundamentals, he tends to get down in the weeds to help advanced users understand C++ more deeply.

  • @pri0ritize
    @pri0ritize 5 месяцев назад

    Seems like the Discount Code isn’t working for renewals.

    • @cppweekly
      @cppweekly  5 месяцев назад

      I'll look into that

    • @cppweekly
      @cppweekly  4 месяца назад

      I reached out to JetBrains and it seems the code is only good for yearly renewals.

  • @314Labs
    @314Labs 5 месяцев назад +1

    How is making an actual generator with co_yield better than using a stateful lambda?

    • @katanasteel
      @katanasteel 5 месяцев назад +1

      Was indeed gonna compare as stateful lambda and coroutines.

    • @kethernet
      @kethernet 5 месяцев назад

      C++ coroutines are implemented essentially like this state machine lambda. Each "co_yield" becomes a state. So they're just a helper to write this kind of state machine, but using straightforward, normal looking code. It's usually easier to see what a generator is doing, than to trace through a bunch of cases in a switch.

    • @sinom
      @sinom 5 месяцев назад

      Usually the response I've heard is "the compiler is able to better optimize coroutines than stateful lambdas" but I have 0 idea how or in what way and coroutines in C++ are (still) a pain to actually get working so for now I'm always just using stateful lambdas when I need stuff like this.

    • @cppweekly
      @cppweekly  4 месяца назад

      I certainly never said it was better to use co_yield!

    • @314Labs
      @314Labs 4 месяца назад

      @@cppweekly you never did, i just assumed :))

  • @TheSenPie
    @TheSenPie 5 месяцев назад

    Any chance you will visit Sweden as well in near future?

    • @cppweekly
      @cppweekly  5 месяцев назад

      I'm happy to go wherever I'm hired to go!

    • @TheSenPie
      @TheSenPie 5 месяцев назад

      @@cppweekly That's the mindset!

  • @greyrabbit2157
    @greyrabbit2157 5 месяцев назад

    amazing