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.
@greenfloatingtoad
10 ай бұрын
Untyped lambda calculus is Turing complete! You can approximate that in C++ by making every parameter auto and running it all at compile time
@Evan490BC
26 күн бұрын
I think you meant to say expressive enough to emulate *closures* , rather than classes.
@axelBr1
Жыл бұрын
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 s, which Lambdas are useful in.
@AschKris
Жыл бұрын
that's what some people who don't like C++ think all C++ is like lol
@psycoder-x
Жыл бұрын
Sorry, but... The more I know about C++, the more I like C.
@iamjadedhobo
Жыл бұрын
@@psycoder-x This lambda code is more readable than some of the 'let`s reinvent iterators' code written by C programmers I have had the honor of inheriting :p
@anon_y_mousse
Жыл бұрын
@@iamjadedhobo I have a feeling you'd like my code. I don't reinvent the wheel, although I do try to optimize it.
@sirhenrystalwart8303
Жыл бұрын
I'll take this nonsense over the macro-based DSL hell that any nontrivial C project ends up with, as the try to re-create standard features of c++.
@UsernameUsername0000
Жыл бұрын
@@psycoder-x Literally how.
@jplflyer
Жыл бұрын
I'm always amazed watching your videos.
@ohwow2074
Жыл бұрын
Could we write the main function as a lamda?
@lalishansh
Жыл бұрын
no we can't, also 😅 that would be crazy as a concept.
@kubastaszak196
Жыл бұрын
flat_map is actually implemented as two vectors, one holding keys, and another holding values.
@ThatGuy-nv2wo
Жыл бұрын
no?
@yntfwyk
Жыл бұрын
@@ThatGuy-nv2wo probably two separate vectors will give better cache coherency.
@cppweekly
Жыл бұрын
std::flat_map is, but mine is not.
@airman122469
11 ай бұрын
“Now I have a new type inside this lambda that has an object with its own lifetime” O_o Whhhhyyyyyyy?
@Taivuttaja
Жыл бұрын
This was beautifully ridiculous. Thanks.
@benjaminshinar9509
Жыл бұрын
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.
@ujin981
Жыл бұрын
Looks like C++ drew inspiration from the dark powers of Perl
@torarinvik4920
Жыл бұрын
LOL :D. Perl is the name and obfuscation is my game!
@12affes
Жыл бұрын
I'm using lambdas instead tuple, highly recommend
@cubeman5303
Жыл бұрын
Can you link some resource/example of this?
@cppweekly
Жыл бұрын
I've seen / played with some of those ideas also
@simple_classic
Жыл бұрын
Ah yes ... jump
@dwarftoad
Жыл бұрын
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.
@0xvector850
Жыл бұрын
> 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?) Haskell?
@torarinvik4920
Жыл бұрын
@@0xvector850 I thought of Haskell too, plus most of the ML family of languages like Ocaml, F#, SML ect.
@anon_y_mousse
Жыл бұрын
Technically, JavaScript allows this. It doesn't force you to define the functions in that way, but you can.
@cppweekly
Жыл бұрын
lisp/scheme doesn't really distinguish between them
@LogicEu
Жыл бұрын
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.
@iverson0389
Жыл бұрын
"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
@nextlifeonearth
Жыл бұрын
The serverless fanatics can pay the hosting bill themselves then.
@skilz8098
Жыл бұрын
This is kind of due to the nature of Lambda Calculus. Lambda Calculus itself is considered to be "turing complete".
@gordonfreimann
Жыл бұрын
sehr Interessant
@DrUlrih
Жыл бұрын
Yep, I made like this: auto connect_with = [closure=[this](auto&& slot, auto&& rs){ ranges::for_each(std::forward(rs), [&slot, this](auto*const r){ connect(r, &QRadioButton::toggled, this, slot); }); }] (auto&& slot, auto&&...ts){ (closure(std::forward(slot), std::forward(ts)), ...); }; And usage: connect_with(&Widget::slot1, a1, a2, a3, a4); connect_with(&Widget::slot2, b1, b2, b3);
@TheMR-777
Жыл бұрын
That's amazing TBH.
@oleksiistri8429
Жыл бұрын
why ?!
@arl-t8d
11 ай бұрын
What am I reading?
@blank-vw2sb
Жыл бұрын
DAMN FIRST COMMENT!!! lets gooo
@__hannibaal__
Жыл бұрын
Why there are no Real Lambdas as key construction like class, struct , …, and function.
@TNothingFree
Жыл бұрын
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.
@cppweekly
Жыл бұрын
Sure, but that takes the fun out of it!
@mjKlaim
Жыл бұрын
LOLZ
@bsdooby
Жыл бұрын
What's the difference between a std::map and a flat_map?
@yntfwyk
Жыл бұрын
flat_map is contiguous in memory whereas regular maps are variations of red-black trees
@bsdooby
Жыл бұрын
Makes sense; so better data locality! THX!
@kippers12isOG
Жыл бұрын
Worse big O performance though, but that's rarely more important.
@user-lv6qm3fj2z
Жыл бұрын
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...
Пікірлер: 51