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

  Рет қаралды 13,487

C++ Weekly With Jason Turner

C++ Weekly With Jason Turner

Ай бұрын

☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
Upcoming Workshop: Understanding Object Lifetime, C++ On Sea, July 2, 2024
► cpponsea.uk/2024/sessions/und...
Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
► ndctechtown.com/workshops/c-b...
CLion is a cross-platform JetBrains IDE for C and C++ with:
- A smart C and C++ editor to navigate and maintain your code base productively.
- Code analysis with quick-fixes to identify and fix bugs and style inconsistencies.
- An integrated debugger - along with other essential tools from the ecosystem - available
straight out of the box.
- And much more!
jb.gg/clion_ide code: CppWeeklyCLion
Episode details: github.com/lefticus/cpp_weekl...
T-SHIRTS AVAILABLE!
► The best C++ T-Shirts anywhere! my-store-d16a2f.creator-sprin...
WANT MORE JASON?
► My Training Classes: emptycrate.com/training.html
► Follow me on twitter: / lefticus
SUPPORT THE CHANNEL
► Patreon: / lefticus
► Github Sponsors: github.com/sponsors/lefticus
► Paypal Donation: www.paypal.com/donate/?hosted...
GET INVOLVED
► Video Idea List: github.com/lefticus/cpp_weekl...
JASON'S BOOKS
► C++23 Best Practices
Leanpub Ebook: leanpub.com/cpp23_best_practi...
► C++ Best Practices
Amazon Paperback: amzn.to/3wpAU3Z
Leanpub Ebook: leanpub.com/cppbestpractices
JASON'S PUZZLE BOOKS
► Object Lifetime Puzzlers Book 1
Amazon Paperback: amzn.to/3g6Ervj
Leanpub Ebook: leanpub.com/objectlifetimepuz...
► Object Lifetime Puzzlers Book 2
Amazon Paperback: amzn.to/3whdUDU
Leanpub Ebook: leanpub.com/objectlifetimepuz...
► Object Lifetime Puzzlers Book 3
Leanpub Ebook: leanpub.com/objectlifetimepuz...
► Copy and Reference Puzzlers Book 1
Amazon Paperback: amzn.to/3g7ZVb9
Leanpub Ebook: leanpub.com/copyandreferencep...
► Copy and Reference Puzzlers Book 2
Amazon Paperback: amzn.to/3X1LOIx
Leanpub Ebook: leanpub.com/copyandreferencep...
► Copy and Reference Puzzlers Book 3
Leanpub Ebook: leanpub.com/copyandreferencep...
► OpCode Puzzlers Book 1
Amazon Paperback: amzn.to/3KCNJg6
Leanpub Ebook: leanpub.com/opcodepuzzlers_book1
RECOMMENDED BOOKS
► Bjarne Stroustrup's A Tour of C++ (now with C++20/23!): amzn.to/3X4Wypr
AWESOME PROJECTS
► The C++ Starter Project - Gets you started with Best Practices Quickly - github.com/cpp-best-practices...
► C++ Best Practices Forkable Coding Standards - github.com/cpp-best-practices...
O'Reilly VIDEOS
► Inheritance and Polymorphism in C++ - www.oreilly.com/library/view/...
► Learning C++ Best Practices - www.oreilly.com/library/view/...

Пікірлер: 52
@kethernet
@kethernet Ай бұрын
Minor nitpick: under "case: Startup", "state = Numbers" should be outside the conditional. As it stands, "3-3" and "33-" parse as -33.
@cppweekly
@cppweekly Ай бұрын
Oops
@MrModez
@MrModez Ай бұрын
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..
@davidesleeds
@davidesleeds Ай бұрын
You finally convinced me to buy the lambda llama shirt!
@Offdopp
@Offdopp Ай бұрын
Haskell devs are giving us the thousand yard stare right now
@alskidan
@alskidan Ай бұрын
Not lazy enough 😂
@anon_y_mousse
@anon_y_mousse Ай бұрын
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. ++++++++++[>+++++++>+.
@KX36
@KX36 Ай бұрын
i was just speculating in my head the other day about implementing coroutines through lambdas just like this.
@cppweekly
@cppweekly 20 күн бұрын
github.com/lefticus/tools/blob/main/include/lefticus/tools/lambda_coroutines.hpp
@ignacionr
@ignacionr Ай бұрын
hmmm that CLion ad
@greyrabbit2157
@greyrabbit2157 Ай бұрын
amazing
@YarikTH
@YarikTH Ай бұрын
It is literally a parser class without any flexibility that class gives you
@turtlesoupage
@turtlesoupage Ай бұрын
It's an example to illustrate the idea.
@YarikTH
@YarikTH Ай бұрын
@@turtlesoupage and I don't see any advantage this idea has over writing a classic C++ class
@AtomicAndi
@AtomicAndi Ай бұрын
Yeah, I definitely missed a hint if/when this makes sense or if it is just toying around
@turtlesoupage
@turtlesoupage Ай бұрын
​@@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 Ай бұрын
@@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.
@N....
@N.... Ай бұрын
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...
@mborows2
@mborows2 Ай бұрын
I wonder what is advantage of using lamda here instead on nromal e.g. IntParser class. Beisdes obfustating the code I don/t see any.
@sebastianschweigert7117
@sebastianschweigert7117 Ай бұрын
How does it obfuscate the code? If you know how to read llambdas then the code isn't any harder to read
@Byynx
@Byynx Ай бұрын
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.
@314Labs
@314Labs Ай бұрын
How is making an actual generator with co_yield better than using a stateful lambda?
@katanasteel
@katanasteel Ай бұрын
Was indeed gonna compare as stateful lambda and coroutines.
@kethernet
@kethernet Ай бұрын
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 Ай бұрын
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 20 күн бұрын
I certainly never said it was better to use co_yield!
@314Labs
@314Labs 20 күн бұрын
@@cppweekly you never did, i just assumed :))
@TheSenPie
@TheSenPie Ай бұрын
Any chance you will visit Sweden as well in near future?
@cppweekly
@cppweekly Ай бұрын
I'm happy to go wherever I'm hired to go!
@TheSenPie
@TheSenPie Ай бұрын
@@cppweekly That's the mindset!
@pri0ritize
@pri0ritize Ай бұрын
Seems like the Discount Code isn’t working for renewals.
@cppweekly
@cppweekly Ай бұрын
I'll look into that
@cppweekly
@cppweekly 24 күн бұрын
I reached out to JetBrains and it seems the code is only good for yearly renewals.
@neozoan
@neozoan Ай бұрын
Reminds me a bit of generators from Python (sorta).
@sirhenrystalwart8303
@sirhenrystalwart8303 Ай бұрын
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.
@eggmeister6641
@eggmeister6641 Ай бұрын
Two porn bots in the comments.
@apenaswellignton
@apenaswellignton Ай бұрын
I always report those, they're so annoying.
@headlibrarian1996
@headlibrarian1996 Ай бұрын
@@apenaswellignton 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 Ай бұрын
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 Ай бұрын
@@cppweekly It wasn't intended as a criticism of you, in case that's how it came across.
@Stasenko58
@Stasenko58 Ай бұрын
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 Ай бұрын
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 Ай бұрын
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.
C++ Weekly - Ep 427 - Simple Generators Without Coroutines
5:27
C++ Weekly With Jason Turner
Рет қаралды 10 М.
C++ Weekly - Ep 363 - A (Complete?) Guide To C++ Unions
16:14
C++ Weekly With Jason Turner
Рет қаралды 13 М.
it takes two to tango 💃🏻🕺🏻
00:18
Zach King
Рет қаралды 32 МЛН
어른의 힘으로만 할 수 있는 버블티 마시는법
00:15
진영민yeongmin
Рет қаралды 12 МЛН
Как быстро замутить ЭлектроСамокат
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 11 МЛН
CAN YOU HELP ME? (ROAD TO 100 MLN!) #shorts
00:26
PANDA BOI
Рет қаралды 36 МЛН
zig is the future of programming. here's why.
9:34
Low Level Learning
Рет қаралды 191 М.
C++ Weekly - Ep 404 - How (and Why) To Write Code That Avoids std::move
8:50
C++ Weekly With Jason Turner
Рет қаралды 24 М.
C++ Weekly - Ep 125 - The Optimal Way To Return From A Function
13:10
C++ Weekly With Jason Turner
Рет қаралды 75 М.
The Pointer to Implementation (pImpl) idiom in C++
6:54
platis.solutions
Рет қаралды 10 М.
C++ Weekly - Ep 312 - Stop Using `constexpr` (And Use This Instead!)
18:24
C++ Weekly With Jason Turner
Рет қаралды 48 М.
how NASA writes space-proof code
6:03
Low Level Learning
Рет қаралды 2 МЛН
SOME UNIQUE C++ CODE! // Pacman Clone Code Review
26:42
The Cherno
Рет қаралды 253 М.
C++ Weekly - Ep 421 - You're Using optional, variant, pair, tuple, any, and expected Wrong!
10:34
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
How much charging is in your phone right now? 📱➡️ 🔋VS 🪫
0:11
POCO F6 PRO - ЛУЧШИЙ POCO НА ДАННЫЙ МОМЕНТ!
18:51
Pratik Cat6 kablo soyma
0:15
Elektrik-Elektronik
Рет қаралды 8 МЛН
Обзор игрового компьютера Макса 2в1
23:34