SlideShare a Scribd company logo
[](){}();
Or How I Learned To Stop Worrying and Love the
Lambda
Pat Viafore
HSV.cpp Meetup
3/15/2017
Lambda
Expressions
Lambda Expressions
Lambda Calculus
Church-Turing Thesis
Conversion and Reduction
Complicated Mathematics
Functional Programming
Underpinnings of Computer Science
Lisp?
Lambda Expressions in C++
Anonymous
Functions
[](){}
[](){}
[](){}
Function
Body
void print(std::function<bool()> func)
{
std::string str = func() ? "true" : "false";
std::cout << str << “n”;
}
auto isFiveGreaterThanThree = [](){ return 5 > 3; };
print(isFiveGreaterThanThree);
[](){}
Parameters
Function
Body
void print(int param, std::function<bool(int)> func)
{
std::string str = func(param) ? "true" : "false";
std::cout << str << “n”;
}
auto isGreaterThanThree = [](int num){ return num > 3; };
print(5, isGreaterThanThree);
[](){}
Capture
List
Parameters
Function
Body
void print(int num, std::function<bool(int)> func)
{
std::string str = func(num) ? "true" : "false";
std::cout << str << “n”;
}
int target = 3;
auto isGreaterThan = [target](int n){ return n > target; };
target = 6;
print(5, isGreaterThan);
Capture List
By Value By Reference
[this]
[a,b]
[=]
[&a,&b]
[&]
Prefer Explicit Captures Over
Default Captures
class A {
public:
int x;
A(): x(10) {}
std::function<bool()> getLambda()
{
return [=](){ return x+2; };
}
};
int main()
{
A* a = new A();
auto lambda = a->getLambda();
delete a;
lambda();
}
This is implicitly this->x. The this
parameter is what gets captured, not
x.
class A {
public:
int x;
A(): x(10) {}
std::function<bool()> getLambda()
{
return [copy=x](){ return copy+2; };
}
};
int main()
{
A* a = new A();
auto lambda = a->getLambda();
delete a;
lambda();
}
C++14 gives us init captures,
where we can initialize the
variables we capture (useful for
any non-local captures)
Why?
Dependency Injection
Pass a function to inject a dependency
Functional Programming Styles
Mapping and Filtering just got a whole lot easier
Algorithm Library
auto isSpecial = [](MacAddress& mac){ return MacAddress::ItuAddress == mac; };
any_of(macs.begin(), macs.end(), isSpecial);
count_if(macs.begin(), macs.end(), isSpecial);
replace_if(macs.begin(), macs.end(), isSpecial, MacAddress::BlankMacAddress);
sort(ips.begin(), ips.end(), [](IpAddressV4& ip1, IpAddressV4& ip2)
{ return ip1.getNetworkByteOrder() < ip2.getNetworkByteOrder()); });
[](){}
Capture
List
Parameters
Function
Body
();
Twitter:
@PatViaforever

More Related Content

What's hot (20)

PDF
TVM の紹介
Masahiro Masuda
 
PDF
デジタル社会の基盤としての地理空間・土地情報
OSgeo Japan
 
PDF
SQLおじさん(自称)がBigQueryのStandard SQLを使ってみた
Kumano Ryo
 
PDF
淺談RESTful API認證 Token機制使用經驗分享
Tun-Yu Chang
 
PPTX
DRIVE CHARTを支えるAI技術
Yusuke Uchida
 
PDF
オープンワールド認識 (第34回全脳アーキテクチャ若手の会 勉強会)
Takuma Yagi
 
PDF
BigQueryの課金、節約しませんか
Ryuji Tamagawa
 
PDF
Word2vecの理論背景
Masato Nakai
 
KEY
アンサンブル学習
Hidekazu Tanaka
 
PDF
オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011
Preferred Networks
 
PPTX
マルチモーダル深層学習の研究動向
Koichiro Mori
 
PPTX
次元の呪い
Kosuke Tsujino
 
PDF
CQRS+ES on GCP
Masaki Toyoshima
 
PDF
Priorに基づく画像/テンソルの復元
Tatsuya Yokota
 
PDF
Pythonによる機械学習
Kimikazu Kato
 
PDF
Rあんなときこんなとき(tokyo r#12)
Shintaro Fukushima
 
PPTX
【論文読み会】PiCO_Contrastive Label Disambiguation for Partial Label Learning.pptx
ARISE analytics
 
PDF
言語と知識の深層学習@認知科学会サマースクール
Yuya Unno
 
PDF
相関係数は傾きに影響される
Mitsuo Shimohata
 
PPTX
深層学習を用いた車内モニタリングによる状態認識に関する研究
harmonylab
 
TVM の紹介
Masahiro Masuda
 
デジタル社会の基盤としての地理空間・土地情報
OSgeo Japan
 
SQLおじさん(自称)がBigQueryのStandard SQLを使ってみた
Kumano Ryo
 
淺談RESTful API認證 Token機制使用經驗分享
Tun-Yu Chang
 
DRIVE CHARTを支えるAI技術
Yusuke Uchida
 
オープンワールド認識 (第34回全脳アーキテクチャ若手の会 勉強会)
Takuma Yagi
 
BigQueryの課金、節約しませんか
Ryuji Tamagawa
 
Word2vecの理論背景
Masato Nakai
 
アンサンブル学習
Hidekazu Tanaka
 
オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011
Preferred Networks
 
マルチモーダル深層学習の研究動向
Koichiro Mori
 
次元の呪い
Kosuke Tsujino
 
CQRS+ES on GCP
Masaki Toyoshima
 
Priorに基づく画像/テンソルの復元
Tatsuya Yokota
 
Pythonによる機械学習
Kimikazu Kato
 
Rあんなときこんなとき(tokyo r#12)
Shintaro Fukushima
 
【論文読み会】PiCO_Contrastive Label Disambiguation for Partial Label Learning.pptx
ARISE analytics
 
言語と知識の深層学習@認知科学会サマースクール
Yuya Unno
 
相関係数は傾きに影響される
Mitsuo Shimohata
 
深層学習を用いた車内モニタリングによる状態認識に関する研究
harmonylab
 

Viewers also liked (17)

PPTX
BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Patrick Viafore
 
PPTX
мадоу № 82
Skazka82
 
PPTX
Presentació pati
Esther Dalmau Palomar
 
DOC
Bcn 3112 note
Yap Lee Hua Lee Hua
 
PPTX
Magazine front covers 4
ah05007283
 
PPTX
Virus360 supersaiyajinlegendario.exe
Mery Noguera
 
PPTX
la comunicación en la sociedad
maria camila suarez betancur
 
PDF
Guide hygiene informatique_anssi
kabengniga ibrahima soro
 
PDF
Industrial Equipment.
Elena Vishnevskaya
 
PDF
Basic functionalities of ez cast dongle
wifi ezcast dongle
 
PDF
TRATAMIENTO AGUA EN HEMODIALISIS
gustavo diaz nuñez
 
PPTX
PRINCIPIOS BIOFISICOS DE LA HEMODIALISIS
gustavo diaz nuñez
 
PDF
Lagopusで試すFW
Tomoya Hibi
 
PPTX
FARMACOCINETICA: ABSORCIÓN Y DISTRIBUCIÓN
evelyn sagredo
 
PPTX
Técnicas del coaching
Rondonjhenyfer
 
PDF
CArcMOOC 06.04 - Dynamic optimizations
Alessandro Bogliolo
 
BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Patrick Viafore
 
мадоу № 82
Skazka82
 
Presentació pati
Esther Dalmau Palomar
 
Bcn 3112 note
Yap Lee Hua Lee Hua
 
Magazine front covers 4
ah05007283
 
Virus360 supersaiyajinlegendario.exe
Mery Noguera
 
la comunicación en la sociedad
maria camila suarez betancur
 
Guide hygiene informatique_anssi
kabengniga ibrahima soro
 
Industrial Equipment.
Elena Vishnevskaya
 
Basic functionalities of ez cast dongle
wifi ezcast dongle
 
TRATAMIENTO AGUA EN HEMODIALISIS
gustavo diaz nuñez
 
PRINCIPIOS BIOFISICOS DE LA HEMODIALISIS
gustavo diaz nuñez
 
Lagopusで試すFW
Tomoya Hibi
 
FARMACOCINETICA: ABSORCIÓN Y DISTRIBUCIÓN
evelyn sagredo
 
Técnicas del coaching
Rondonjhenyfer
 
CArcMOOC 06.04 - Dynamic optimizations
Alessandro Bogliolo
 
Ad

Similar to Lambda Expressions in C++ (20)

PPTX
The Arrow Library in Kotlin
Garth Gilmour
 
PDF
Developer Experience i TypeScript. Najbardziej ikoniczne duo
The Software House
 
PPTX
Templates in C++
Tech_MX
 
PPS
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
PPTX
Library functions in c++
Neeru Mittal
 
PPTX
Fun with Lambdas: C++14 Style (part 2)
Sumant Tambe
 
PPT
An imperative study of c
Tushar B Kute
 
PPTX
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
DOCX
SaraPIC
Sara Sahu
 
PPTX
Input output functions
hyderali123
 
PPTX
NetPonto - The Future Of C# - NetConf Edition
Paulo Morgado
 
PPT
Csharp4 delegates lambda_and_events
Abed Bukhari
 
PDF
Alexey Tsoy Meta Programming in C++ 16.11.17
LogeekNightUkraine
 
PPS
CS101- Introduction to Computing- Lecture 38
Bilal Ahmed
 
PPT
Unit 5 Foc
JAYA
 
PPTX
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
PDF
Apache Commons - Don\'t re-invent the wheel
tcurdt
 
PPSX
Scala @ TomTom
Eric Bowman
 
DOCX
.net progrmming part1
Dr.M.Karthika parthasarathy
 
The Arrow Library in Kotlin
Garth Gilmour
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
The Software House
 
Templates in C++
Tech_MX
 
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
Library functions in c++
Neeru Mittal
 
Fun with Lambdas: C++14 Style (part 2)
Sumant Tambe
 
An imperative study of c
Tushar B Kute
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
SaraPIC
Sara Sahu
 
Input output functions
hyderali123
 
NetPonto - The Future Of C# - NetConf Edition
Paulo Morgado
 
Csharp4 delegates lambda_and_events
Abed Bukhari
 
Alexey Tsoy Meta Programming in C++ 16.11.17
LogeekNightUkraine
 
CS101- Introduction to Computing- Lecture 38
Bilal Ahmed
 
Unit 5 Foc
JAYA
 
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
Apache Commons - Don\'t re-invent the wheel
tcurdt
 
Scala @ TomTom
Eric Bowman
 
.net progrmming part1
Dr.M.Karthika parthasarathy
 
Ad

More from Patrick Viafore (12)

PPTX
Extensible Python: Robustness through Addition - PyCon 2024
Patrick Viafore
 
PDF
User-Defined Types.pdf
Patrick Viafore
 
PDF
The Most Misunderstood Line In Zen Of Python.pdf
Patrick Viafore
 
PPTX
Robust Python.pptx
Patrick Viafore
 
PDF
Tip Top Typing - A Look at Python Typing
Patrick Viafore
 
PPTX
RunC, Docker, RunC
Patrick Viafore
 
PDF
DevSpace 2018 - Practical Computer Science: What You Need To Know Without Th...
Patrick Viafore
 
PPTX
Controlling Raspberry Pis With Your Phone Using Python
Patrick Viafore
 
PDF
C++17 not your father’s c++
Patrick Viafore
 
PPTX
Building a development community within your workplace
Patrick Viafore
 
PPTX
Hsv.py Lightning Talk - Bottle
Patrick Viafore
 
PPTX
Controlling the browser through python and selenium
Patrick Viafore
 
Extensible Python: Robustness through Addition - PyCon 2024
Patrick Viafore
 
User-Defined Types.pdf
Patrick Viafore
 
The Most Misunderstood Line In Zen Of Python.pdf
Patrick Viafore
 
Robust Python.pptx
Patrick Viafore
 
Tip Top Typing - A Look at Python Typing
Patrick Viafore
 
RunC, Docker, RunC
Patrick Viafore
 
DevSpace 2018 - Practical Computer Science: What You Need To Know Without Th...
Patrick Viafore
 
Controlling Raspberry Pis With Your Phone Using Python
Patrick Viafore
 
C++17 not your father’s c++
Patrick Viafore
 
Building a development community within your workplace
Patrick Viafore
 
Hsv.py Lightning Talk - Bottle
Patrick Viafore
 
Controlling the browser through python and selenium
Patrick Viafore
 

Recently uploaded (20)

PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 

Lambda Expressions in C++

  • 1. [](){}(); Or How I Learned To Stop Worrying and Love the Lambda Pat Viafore HSV.cpp Meetup 3/15/2017
  • 3. Lambda Expressions Lambda Calculus Church-Turing Thesis Conversion and Reduction Complicated Mathematics Functional Programming Underpinnings of Computer Science Lisp?
  • 9. void print(std::function<bool()> func) { std::string str = func() ? "true" : "false"; std::cout << str << “n”; } auto isFiveGreaterThanThree = [](){ return 5 > 3; }; print(isFiveGreaterThanThree);
  • 11. void print(int param, std::function<bool(int)> func) { std::string str = func(param) ? "true" : "false"; std::cout << str << “n”; } auto isGreaterThanThree = [](int num){ return num > 3; }; print(5, isGreaterThanThree);
  • 13. void print(int num, std::function<bool(int)> func) { std::string str = func(num) ? "true" : "false"; std::cout << str << “n”; } int target = 3; auto isGreaterThan = [target](int n){ return n > target; }; target = 6; print(5, isGreaterThan);
  • 14. Capture List By Value By Reference [this] [a,b] [=] [&a,&b] [&] Prefer Explicit Captures Over Default Captures
  • 15. class A { public: int x; A(): x(10) {} std::function<bool()> getLambda() { return [=](){ return x+2; }; } }; int main() { A* a = new A(); auto lambda = a->getLambda(); delete a; lambda(); } This is implicitly this->x. The this parameter is what gets captured, not x.
  • 16. class A { public: int x; A(): x(10) {} std::function<bool()> getLambda() { return [copy=x](){ return copy+2; }; } }; int main() { A* a = new A(); auto lambda = a->getLambda(); delete a; lambda(); } C++14 gives us init captures, where we can initialize the variables we capture (useful for any non-local captures)
  • 17. Why? Dependency Injection Pass a function to inject a dependency Functional Programming Styles Mapping and Filtering just got a whole lot easier Algorithm Library auto isSpecial = [](MacAddress& mac){ return MacAddress::ItuAddress == mac; }; any_of(macs.begin(), macs.end(), isSpecial); count_if(macs.begin(), macs.end(), isSpecial); replace_if(macs.begin(), macs.end(), isSpecial, MacAddress::BlankMacAddress); sort(ips.begin(), ips.end(), [](IpAddressV4& ip1, IpAddressV4& ip2) { return ip1.getNetworkByteOrder() < ip2.getNetworkByteOrder()); });