fenbf / cppstories-discussions

4 stars 1 forks source link

2021/lambda-story-print/ #4

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

C++ Lambda Story in Print - C++ Stories

I’m happy to announce the print/paperback version of C++ Lambda Story! After more than a year of updates and smaller changes, the whole project is completed! You can now purchase the book in lots of different formats. See details of this major update and also take part in a giveaway and get the book for free :)

https://www.cppstories.com/2021/lambda-story-print/

WolfgangVogl commented 3 years ago

Hello, I already have a bit of experience using Lambdas. I use lambdas in very many, different areas: In particular, I use lambdas to do complex initializations (C++ Core Guidelines, ES.28) directly (as IIFE) and also to refactor legacy code in terms of readability. I often use nice additions (via a template) like [[nodiscard]] for lambdas in the process.

fenbf commented 3 years ago

Initially written by @xventura81 on https://github.com/fenbf/cppstories-discussions/issues/5

Lambdas are great! I like to use them for instance in Qt when defining a slot in a QObject::connect call. Example:

QApplication::connect( buttonInfo, &QPushButton::clicked,
                      [] { std::cout << "Button clicked." << std::endl; });
ledzepFan commented 3 years ago

I would still deem myself a beginner at Lambda usage, though it was something that has been of immense help. At my previous company we used a lot of function pointers in our code. I had two big grouches with that:

  1. The syntax of function pointers is not very convenient to digest.
  2. The functions needed to have a context object passed in in order to appropriately cast to the correct object that had registered the callback.

These two were, to a great extent solved by Lambda expressions coupled with std::function. It made the code a lot more localized, in that, it was easy to see what the callback was supposed to do at the same point of registration of the callback.

Once I'd discovered this, I've used Lambdas a lot in the STL algorithms like std::sort etc.

I'm still coming to grips with the nuances of using this and *this with every C++ standard. :D

KrzysztofKulis commented 3 years ago

Have already been using lambdas for a while, but I'm aiming for better understanding of these! I tend to use them coupled with std features requiring predicates and when I want to make some blocks of code concise, through having functions appropriately scoped.

mabrouk2005 commented 3 years ago

I am a beginner and want to start using lambdas in my school assignment programs. thanks for your work.

orthopteroid commented 3 years ago

One of the most useful places I've used a light sprinkling of them are for FSM state handlers - it allows much of my machinery to be written within the static declaration of an FSM table. Object pascal has a similar lambda mechanism and I've found that the same pattern of usage really helps with readability there as well.

Sumit-Gandhi commented 3 years ago

I have been using lambda for few years now. They are great evolution in Modern C++ replacing function pointers and functors. I mainly use them in creating comparators in C++ algorithms and Replacing function pointers in our code with lambdas.

thomasoatman commented 3 years ago

Hi. I have been coding since the mid 80's but have been stuck in old code. I recently had a project that allowed me to explore a bit and I am blown away by amount of cool-ness in C++ now. While looking for information, I came across Bartek's site. I plan to look through as much as I can when the project settles down. Anyway, I am terribly new to lambdas. I have seen them used a couple times (mainly in Python) and been a bit frazzeled at some of the syntax that seems to go against my previous thought of C++. I was super excited to learn about the finally() syntax. I have utilized lambdas for this purpose to automate cleanup of resources in case of mishaps. (I said I am in old code) This will have to do until I rewrite everything into classes. This old dog looks forward to learning some new tricks!

p.s. thanks for the Color option. I, for one, can internalize code much better in color (and I'm not talking about Amber LOL!)

sergio-nsk commented 3 years ago

As an experienced developer, I often use lambdas for not trivial initialisations of static or global constant variables, but of course not limited by this case. For example:

#include <iostream>
#include <string>
#include <ctime>

const std::string starttime = [] () {
  const auto time = std::time(nullptr);
  return std::ctime(&time);
}();

int main() {
  std::cout << "App start time: " << starttime << std::endl;
}
cmami commented 3 years ago

I am an experienced C++ developer. I use lambdas in various situations, but would like to raise two valuable and fun usages:

josephedwardchang commented 3 years ago

I am a beginner of lambdas on C++, but quite a veteran of lambdas in C#, that's why I bought this book (pdf) to learn more on how they differ and how I can leverage my lambda experiences in C# for my C++ projects. Currently I am learning also C++17. I like this lambda book because I can see the development of lambdas (their history) and their usage from C++11 to the current ones. And that gives me insight on how they can be maximized

I mainly use lambdas for filtering list of objects in csharp combined with LINQ:

var itemsWithId = LatestData.Where(item => item.Id == latest.Id && item.Id != null);

the result above: itemsWithId will only contain items from LatestData whose Id is the same as latest.Id and not null. WIth lambdas I can shorten my code (no matter how complex the logic is) to 1 line and still be readable. Now I'm still learning how to do this in C++, mind you :D

mskrzypkows commented 3 years ago

Most frequently I use lambdas to run concurrent code by few threads. I start a few threads and everyone takes some part of the data to process.

soyeb commented 3 years ago

My most recent use of lambdas is to register as a callback.