C++ Weekly - Ep 384 - Lambda-Only Programming

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

This reminds me that the SICP book implements a whole OOP system using lambdas (in a language that doesn't support OOP natively). We like to think lambdas as syntactic sugar of classes, but they are also expressive enough to emulate classes.

LesleyLai
Автор

I started programming in C++ in the early 2000s, and due to work came back to it late 2019. A year before this I'd been invited to attend the computing club of a local uni's Friday night talk, and all the students were raving about Functional Programming and Haskel. And as they showed Haskel code examples I wondered how anyone would be able to work out what the hell was supposed to be happening in even a small program.

I have started using Lambdas in my code, but don't find them all the clear to aid understand what is happening, still think that a function is easier to understand and easier to reuse; although I'm getting to grips with ranged based For loops and <algorithm>s, which Lambdas are useful in.

axelBr
Автор

I'm always amazed watching your videos.

jplflyer
Автор

Could we write the main function as a lamda?

ohwow
Автор

I'm missing something here with the variadic parameters pack.
what happens if you pass two input keys? does it return the data itself? the `sizeof...(input_key)` won't be 1.

benjaminshinar
Автор

flat_map is actually implemented as two vectors, one holding keys, and another holding values.

kubastaszak
Автор

that's what some people who don't like C++ think all C++ is like lol

AschKris
Автор

This was beautifully ridiculous. Thanks.

Taivuttaja
Автор

Looks like C++ drew inspiration from the dark powers of Perl

ujin
Автор

“Now I have a new type inside this lambda that has an object with its own lifetime”

O_o


airman
Автор

I'm using lambdas instead tuple<>, highly recommend

affes
Автор

Lambdas inside captures of lambdas?? What? Hahah, that's pretty cool, but I don't know how really useful or necessary, it gets pretty ugly too.

LogicEu
Автор

No reason that some language couldn't have you define both functions and variables in the same general way (same syntax). (Anyone know of one that does?) [ I guess in that language you could also only allow user defined types and new variables to be defined as part of the lambda expression like in your examples but that does make programming kind of hard :) ]

The C-like function definition blocks that we are familiar with could be thought of as just a stand in for a sequence of instructions wrapped in standard stack handling and returning code that will be generated during compilation. Imagine the text of the function prologue with argument names and types just replaced directly with stack handling code, and variable declarations in the function with stack management code for the variable, and then the closing brace and return statements replaced with cleanup/return epilogue code.

Remember that older computers had very limited memory, so early compilers could, perhaps, just write output to disk or other storage while interpreting the source files rather than storing a lot of intermediate code and other information in memory, i.e. A very simplistic C compiler can just parse the function prologue, write the stack handling machine code, parse and translate each statement in the function to machine code (including variable declarations pushed to the stack), with a relative minimum of state required to be kept as it parses, until the end of the function.

dwarftoad
Автор

"You can just use lambdas for literally everything" - This quote rings too true... not for C++ but for AWS and me who has to deal with serverless fanatics everyday

iverson
Автор

What's the difference between a std::map and a flat_map?

bsdooby
Автор

Yep, I made like this:

auto connect_with = [closure=[this](auto&& slot, auto&& rs){ ranges::for_each(std::forward<decltype(rs)>(rs), [&slot, this](auto*const r){ connect(r, &QRadioButton::toggled, this, slot); }); }] (auto&& slot, auto&&...ts){ (closure(std::forward<decltype(slot)>(slot), std::forward<decltype(ts)>(ts)), ...); };

And usage:

connect_with(&Widget::slot1, a1, a2, a3, a4);
connect_with(&Widget::slot2, b1, b2, b3);

DrUlrih
Автор

Why there are no Real Lambdas as key construction like class, struct, …, and function.

__hannibaal__
Автор

Hear me out...
Lambda is creating a class behind the scenes.
So technically, just technically. this is oop.

I understand more and more why people are adapting Lambdas, they are very powerful especially for async behaviors.

TNothingFree
Автор

If I ever encounter something like that in a code - I'll just burn down the building and hope everyone thinks it was an accident. And then I'll go live in Argentina or something...

lvqmfjz