Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++...
Transcript of Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++...
![Page 1: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/1.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Functional Programming in C++C++ Support for Functional Paradigm
Bartosz Kwaśniewski
Bartosz Kwaśniewski Functional Programming in C++ 1 / 131
![Page 2: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/2.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Hi did it for all of us ;)
Bjarne Stroustrup
“I wonder what would happen, ifthere were a language socomplicated, so difficult to learn,that nobody would ever be able toswamp the market withprogrammers”
Bartosz Kwaśniewski Functional Programming in C++ 2 / 131
![Page 3: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/3.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Hi did it for all of us ;)
Bjarne Stroustrup
“I wonder what would happen, ifthere were a language socomplicated, so difficult to learn,that nobody would ever be able toswamp the market withprogrammers”
Bartosz Kwaśniewski Functional Programming in C++ 2 / 131
![Page 4: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/4.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Claims and Questions
▶ Claims:
▶ C++ supports or has just recently started to supportfunctional programming
▶ Questions:
▶ Is it true, is it really true?▶ What does it mean?
Bartosz Kwaśniewski Functional Programming in C++ 3 / 131
![Page 5: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/5.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Claims and Questions
▶ Claims:▶ C++ supports or has just recently started to support
functional programming
▶ Questions:
▶ Is it true, is it really true?▶ What does it mean?
Bartosz Kwaśniewski Functional Programming in C++ 3 / 131
![Page 6: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/6.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Claims and Questions
▶ Claims:▶ C++ supports or has just recently started to support
functional programming▶ Questions:
▶ Is it true, is it really true?▶ What does it mean?
Bartosz Kwaśniewski Functional Programming in C++ 3 / 131
![Page 7: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/7.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Claims and Questions
▶ Claims:▶ C++ supports or has just recently started to support
functional programming▶ Questions:
▶ Is it true, is it really true?
▶ What does it mean?
Bartosz Kwaśniewski Functional Programming in C++ 3 / 131
![Page 8: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/8.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Claims and Questions
▶ Claims:▶ C++ supports or has just recently started to support
functional programming▶ Questions:
▶ Is it true, is it really true?▶ What does it mean?
Bartosz Kwaśniewski Functional Programming in C++ 3 / 131
![Page 9: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/9.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Key Messages
1. Paradigm shift towards functional programming isnecessary and has already started
2. You could use many styles including objectoriented and functional to make better software
3. There is no other option, no turning back: youmust learn the functional paradigm
Bartosz Kwaśniewski Functional Programming in C++ 4 / 131
![Page 10: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/10.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Key Messages
1. Paradigm shift towards functional programming isnecessary and has already started
2. You could use many styles including objectoriented and functional to make better software
3. There is no other option, no turning back: youmust learn the functional paradigm
Bartosz Kwaśniewski Functional Programming in C++ 4 / 131
![Page 11: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/11.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Key Messages
1. Paradigm shift towards functional programming isnecessary and has already started
2. You could use many styles including objectoriented and functional to make better software
3. There is no other option, no turning back: youmust learn the functional paradigm
Bartosz Kwaśniewski Functional Programming in C++ 4 / 131
![Page 12: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/12.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
But, Why?
▶ Why?
▶ Moore’s law (free lunch is over)▶ Heat wall, no more clock race (max 3.4GHz)▶ Multi-core arrival (solution)▶ Concurrent software (require redesign)▶ Object oriented pitfalls▶ Composability (concurrency not scale well)▶ Different approach: functional programming (purity,
immutability)
Bartosz Kwaśniewski Functional Programming in C++ 5 / 131
![Page 13: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/13.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
But, Why?
▶ Why?▶ Moore’s law (free lunch is over)
▶ Heat wall, no more clock race (max 3.4GHz)▶ Multi-core arrival (solution)▶ Concurrent software (require redesign)▶ Object oriented pitfalls▶ Composability (concurrency not scale well)▶ Different approach: functional programming (purity,
immutability)
Bartosz Kwaśniewski Functional Programming in C++ 5 / 131
![Page 14: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/14.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
But, Why?
▶ Why?▶ Moore’s law (free lunch is over)▶ Heat wall, no more clock race (max 3.4GHz)
▶ Multi-core arrival (solution)▶ Concurrent software (require redesign)▶ Object oriented pitfalls▶ Composability (concurrency not scale well)▶ Different approach: functional programming (purity,
immutability)
Bartosz Kwaśniewski Functional Programming in C++ 5 / 131
![Page 15: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/15.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
But, Why?
▶ Why?▶ Moore’s law (free lunch is over)▶ Heat wall, no more clock race (max 3.4GHz)▶ Multi-core arrival (solution)
▶ Concurrent software (require redesign)▶ Object oriented pitfalls▶ Composability (concurrency not scale well)▶ Different approach: functional programming (purity,
immutability)
Bartosz Kwaśniewski Functional Programming in C++ 5 / 131
![Page 16: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/16.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
But, Why?
▶ Why?▶ Moore’s law (free lunch is over)▶ Heat wall, no more clock race (max 3.4GHz)▶ Multi-core arrival (solution)▶ Concurrent software (require redesign)
▶ Object oriented pitfalls▶ Composability (concurrency not scale well)▶ Different approach: functional programming (purity,
immutability)
Bartosz Kwaśniewski Functional Programming in C++ 5 / 131
![Page 17: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/17.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
But, Why?
▶ Why?▶ Moore’s law (free lunch is over)▶ Heat wall, no more clock race (max 3.4GHz)▶ Multi-core arrival (solution)▶ Concurrent software (require redesign)▶ Object oriented pitfalls
▶ Composability (concurrency not scale well)▶ Different approach: functional programming (purity,
immutability)
Bartosz Kwaśniewski Functional Programming in C++ 5 / 131
![Page 18: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/18.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
But, Why?
▶ Why?▶ Moore’s law (free lunch is over)▶ Heat wall, no more clock race (max 3.4GHz)▶ Multi-core arrival (solution)▶ Concurrent software (require redesign)▶ Object oriented pitfalls▶ Composability (concurrency not scale well)
▶ Different approach: functional programming (purity,immutability)
Bartosz Kwaśniewski Functional Programming in C++ 5 / 131
![Page 19: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/19.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
But, Why?
▶ Why?▶ Moore’s law (free lunch is over)▶ Heat wall, no more clock race (max 3.4GHz)▶ Multi-core arrival (solution)▶ Concurrent software (require redesign)▶ Object oriented pitfalls▶ Composability (concurrency not scale well)▶ Different approach: functional programming (purity,
immutability)
Bartosz Kwaśniewski Functional Programming in C++ 5 / 131
![Page 20: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/20.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Really smart guy’s says“The problem with object-oriented languages is that if you wanted abanana but what you got was a gorilla holding the banana and theentire jungle”
Joe Armstrong, Erlang Creator
“It was only supposed to be a joke. Anyone with half a brain can seethat object-oriented programming is counter-intuitive, illogical andinefficient”
Bjarne Stroustrup
“When the limestone of imperative programming is worn away, thegranite of functional programming will be observed.”
Simon Peyton Jones (GHC)
Bartosz Kwaśniewski Functional Programming in C++ 6 / 131
![Page 21: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/21.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Really smart guy’s says“The problem with object-oriented languages is that if you wanted abanana but what you got was a gorilla holding the banana and theentire jungle”
Joe Armstrong, Erlang Creator
“It was only supposed to be a joke. Anyone with half a brain can seethat object-oriented programming is counter-intuitive, illogical andinefficient”
Bjarne Stroustrup
“When the limestone of imperative programming is worn away, thegranite of functional programming will be observed.”
Simon Peyton Jones (GHC)
Bartosz Kwaśniewski Functional Programming in C++ 6 / 131
![Page 22: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/22.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Really smart guy’s says“The problem with object-oriented languages is that if you wanted abanana but what you got was a gorilla holding the banana and theentire jungle”
Joe Armstrong, Erlang Creator
“It was only supposed to be a joke. Anyone with half a brain can seethat object-oriented programming is counter-intuitive, illogical andinefficient”
Bjarne Stroustrup
“When the limestone of imperative programming is worn away, thegranite of functional programming will be observed.”
Simon Peyton Jones (GHC)
Bartosz Kwaśniewski Functional Programming in C++ 6 / 131
![Page 23: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/23.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Introduction
Table of Contents
1 Introduction
2 Programming Paradigms
3 Functional Programming
4 C++ Metaprogramming as Functional Language
5 Conclusion
Bartosz Kwaśniewski Functional Programming in C++ 7 / 131
![Page 24: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/24.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
About Programming Paradigms
▶ What does it mean that a language supports aparadigm well?
Bartosz Kwaśniewski Functional Programming in C++ 8 / 131
![Page 25: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/25.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
About Programming Paradigms▶ What does it mean that a language supports aparadigm well?
Bartosz Kwaśniewski Functional Programming in C++ 8 / 131
![Page 26: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/26.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Paradigm Definition
▶ What is a paradigm?
Programming style or approach based on a co-herent set of programming concepts aimed to solveparticular kind of problem.
Bartosz Kwaśniewski Functional Programming in C++ 9 / 131
![Page 27: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/27.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Paradigm Definition
▶ What is a paradigm?
Programming style or approach based on a co-herent set of programming concepts aimed to solveparticular kind of problem.
Bartosz Kwaśniewski Functional Programming in C++ 9 / 131
![Page 28: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/28.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Programming Languages, Paradigms and Concepts
Bartosz Kwaśniewski Functional Programming in C++ 10 / 131
![Page 29: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/29.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Multiparadigm Programming
▶ What is Multiparadigm programming?
1. Programming using more than one programming style,each to its best effect
2. The same as just ”programming” but using differentfeatures in combination as needed
▶ Why do we need multiparagim languages?
1. Real and different problems need differentprogramming concepts to solve them cleanly
2. However: this requires the expetise level
Bartosz Kwaśniewski Functional Programming in C++ 11 / 131
![Page 30: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/30.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Multiparadigm Programming
▶ What is Multiparadigm programming?1. Programming using more than one programming style,
each to its best effect
2. The same as just ”programming” but using differentfeatures in combination as needed
▶ Why do we need multiparagim languages?
1. Real and different problems need differentprogramming concepts to solve them cleanly
2. However: this requires the expetise level
Bartosz Kwaśniewski Functional Programming in C++ 11 / 131
![Page 31: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/31.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Multiparadigm Programming
▶ What is Multiparadigm programming?1. Programming using more than one programming style,
each to its best effect2. The same as just ”programming” but using different
features in combination as needed
▶ Why do we need multiparagim languages?
1. Real and different problems need differentprogramming concepts to solve them cleanly
2. However: this requires the expetise level
Bartosz Kwaśniewski Functional Programming in C++ 11 / 131
![Page 32: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/32.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Multiparadigm Programming
▶ What is Multiparadigm programming?1. Programming using more than one programming style,
each to its best effect2. The same as just ”programming” but using different
features in combination as needed▶ Why do we need multiparagim languages?
1. Real and different problems need differentprogramming concepts to solve them cleanly
2. However: this requires the expetise level
Bartosz Kwaśniewski Functional Programming in C++ 11 / 131
![Page 33: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/33.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Multiparadigm Programming
▶ What is Multiparadigm programming?1. Programming using more than one programming style,
each to its best effect2. The same as just ”programming” but using different
features in combination as needed▶ Why do we need multiparagim languages?
1. Real and different problems need differentprogramming concepts to solve them cleanly
2. However: this requires the expetise level
Bartosz Kwaśniewski Functional Programming in C++ 11 / 131
![Page 34: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/34.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Multiparadigm Programming
▶ What is Multiparadigm programming?1. Programming using more than one programming style,
each to its best effect2. The same as just ”programming” but using different
features in combination as needed▶ Why do we need multiparagim languages?
1. Real and different problems need differentprogramming concepts to solve them cleanly
2. However: this requires the expetise level
Bartosz Kwaśniewski Functional Programming in C++ 11 / 131
![Page 35: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/35.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Paradigm Support: Conclusion
▶ Good (just) support:
1. Convenient, reasonably easy, safe, and efficient2. Built-in facilities, standard libraries
▶ No (merely) support:
1. Exceptional effort, skill, struggle2. Work arounds, no libraries, custom solutions, external
tools and libraries3. Only self-discipline
Bartosz Kwaśniewski Functional Programming in C++ 12 / 131
![Page 36: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/36.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Paradigm Support: Conclusion
▶ Good (just) support:1. Convenient, reasonably easy, safe, and efficient
2. Built-in facilities, standard libraries▶ No (merely) support:
1. Exceptional effort, skill, struggle2. Work arounds, no libraries, custom solutions, external
tools and libraries3. Only self-discipline
Bartosz Kwaśniewski Functional Programming in C++ 12 / 131
![Page 37: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/37.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Paradigm Support: Conclusion
▶ Good (just) support:1. Convenient, reasonably easy, safe, and efficient2. Built-in facilities, standard libraries
▶ No (merely) support:
1. Exceptional effort, skill, struggle2. Work arounds, no libraries, custom solutions, external
tools and libraries3. Only self-discipline
Bartosz Kwaśniewski Functional Programming in C++ 12 / 131
![Page 38: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/38.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Paradigm Support: Conclusion
▶ Good (just) support:1. Convenient, reasonably easy, safe, and efficient2. Built-in facilities, standard libraries
▶ No (merely) support:
1. Exceptional effort, skill, struggle2. Work arounds, no libraries, custom solutions, external
tools and libraries3. Only self-discipline
Bartosz Kwaśniewski Functional Programming in C++ 12 / 131
![Page 39: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/39.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Paradigm Support: Conclusion
▶ Good (just) support:1. Convenient, reasonably easy, safe, and efficient2. Built-in facilities, standard libraries
▶ No (merely) support:1. Exceptional effort, skill, struggle
2. Work arounds, no libraries, custom solutions, externaltools and libraries
3. Only self-discipline
Bartosz Kwaśniewski Functional Programming in C++ 12 / 131
![Page 40: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/40.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Paradigm Support: Conclusion
▶ Good (just) support:1. Convenient, reasonably easy, safe, and efficient2. Built-in facilities, standard libraries
▶ No (merely) support:1. Exceptional effort, skill, struggle2. Work arounds, no libraries, custom solutions, external
tools and libraries
3. Only self-discipline
Bartosz Kwaśniewski Functional Programming in C++ 12 / 131
![Page 41: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/41.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Programming Paradigms
Paradigm Support: Conclusion
▶ Good (just) support:1. Convenient, reasonably easy, safe, and efficient2. Built-in facilities, standard libraries
▶ No (merely) support:1. Exceptional effort, skill, struggle2. Work arounds, no libraries, custom solutions, external
tools and libraries3. Only self-discipline
Bartosz Kwaśniewski Functional Programming in C++ 12 / 131
![Page 42: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/42.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming
Functional Programming
▶ How does C++ implement functional concepts?
Bartosz Kwaśniewski Functional Programming in C++ 13 / 131
![Page 43: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/43.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming
Functional Programming▶ How does C++ implement functional concepts?
Bartosz Kwaśniewski Functional Programming in C++ 13 / 131
![Page 44: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/44.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Language
▶ What is a functional language?
It is a language that emphasises programmingwith pure functions and immutable data withthe control of side effects and mutability.
▶ How the solution is expressed in functional program?
The program is defined by declarations or ex-pression, not described by the series of steps.
Bartosz Kwaśniewski Functional Programming in C++ 14 / 131
![Page 45: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/45.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Language
▶ What is a functional language?
It is a language that emphasises programmingwith pure functions and immutable data withthe control of side effects and mutability.
▶ How the solution is expressed in functional program?
The program is defined by declarations or ex-pression, not described by the series of steps.
Bartosz Kwaśniewski Functional Programming in C++ 14 / 131
![Page 46: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/46.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Language
▶ What is a functional language?
It is a language that emphasises programmingwith pure functions and immutable data withthe control of side effects and mutability.
▶ How the solution is expressed in functional program?
The program is defined by declarations or ex-pression, not described by the series of steps.
Bartosz Kwaśniewski Functional Programming in C++ 14 / 131
![Page 47: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/47.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Language
▶ What is a functional language?
It is a language that emphasises programmingwith pure functions and immutable data withthe control of side effects and mutability.
▶ How the solution is expressed in functional program?
The program is defined by declarations or ex-pression, not described by the series of steps.
Bartosz Kwaśniewski Functional Programming in C++ 14 / 131
![Page 48: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/48.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Characteristics
1. Focusing on what rather on how
2. The program is defined in more mathematical sense3. The functions are the primary constructs4. The mutability and side effects are controlled5. The operator = is an equivalence operator6. There is substitution model
Bartosz Kwaśniewski Functional Programming in C++ 15 / 131
![Page 49: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/49.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Characteristics
1. Focusing on what rather on how2. The program is defined in more mathematical sense
3. The functions are the primary constructs4. The mutability and side effects are controlled5. The operator = is an equivalence operator6. There is substitution model
Bartosz Kwaśniewski Functional Programming in C++ 15 / 131
![Page 50: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/50.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Characteristics
1. Focusing on what rather on how2. The program is defined in more mathematical sense3. The functions are the primary constructs
4. The mutability and side effects are controlled5. The operator = is an equivalence operator6. There is substitution model
Bartosz Kwaśniewski Functional Programming in C++ 15 / 131
![Page 51: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/51.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Characteristics
1. Focusing on what rather on how2. The program is defined in more mathematical sense3. The functions are the primary constructs4. The mutability and side effects are controlled
5. The operator = is an equivalence operator6. There is substitution model
Bartosz Kwaśniewski Functional Programming in C++ 15 / 131
![Page 52: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/52.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Characteristics
1. Focusing on what rather on how2. The program is defined in more mathematical sense3. The functions are the primary constructs4. The mutability and side effects are controlled5. The operator = is an equivalence operator
6. There is substitution model
Bartosz Kwaśniewski Functional Programming in C++ 15 / 131
![Page 53: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/53.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Characteristics
1. Focusing on what rather on how2. The program is defined in more mathematical sense3. The functions are the primary constructs4. The mutability and side effects are controlled5. The operator = is an equivalence operator6. There is substitution model
Bartosz Kwaśniewski Functional Programming in C++ 15 / 131
![Page 54: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/54.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Foundations
▶ What are the math foundation?
1. λ-Calculus2. Combinatory logic3. Category theory
▶ No, we don’t need to know that to write functionalprograms
Bartosz Kwaśniewski Functional Programming in C++ 16 / 131
![Page 55: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/55.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Foundations
▶ What are the math foundation?1. λ-Calculus
2. Combinatory logic3. Category theory
▶ No, we don’t need to know that to write functionalprograms
Bartosz Kwaśniewski Functional Programming in C++ 16 / 131
![Page 56: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/56.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Foundations
▶ What are the math foundation?1. λ-Calculus2. Combinatory logic
3. Category theory▶ No, we don’t need to know that to write functionalprograms
Bartosz Kwaśniewski Functional Programming in C++ 16 / 131
![Page 57: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/57.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Foundations
▶ What are the math foundation?1. λ-Calculus2. Combinatory logic3. Category theory
▶ No, we don’t need to know that to write functionalprograms
Bartosz Kwaśniewski Functional Programming in C++ 16 / 131
![Page 58: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/58.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Foundations
▶ What are the math foundation?1. λ-Calculus2. Combinatory logic3. Category theory
▶ No, we don’t need to know that to write functionalprograms
Bartosz Kwaśniewski Functional Programming in C++ 16 / 131
![Page 59: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/59.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:
1. Modularity and composability2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:
1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 60: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/60.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability
2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:
1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 61: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/61.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability2. Equational reasoning and proofability of correctness
3. Inherent parallelism and concurrency4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:
1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 62: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/62.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency
4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:
1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 63: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/63.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency4. More consist and shorter code
5. Automatic memory management▶ Disadvantages:
1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 64: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/64.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:
1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 65: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/65.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:
1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 66: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/66.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:1. Hard to learn for developers used to imperative languages
2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 67: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/67.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 68: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/68.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%
2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 69: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/69.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Features
▶ Advantages:1. Modularity and composability2. Equational reasoning and proofability of correctness3. Inherent parallelism and concurrency4. More consist and shorter code5. Automatic memory management
▶ Disadvantages:1. Hard to learn for developers used to imperative languages2. Not mainstream paradigm and language (TIOBE Index)
2.1 Java - 18.7%, C - 9.6%, C++ - 5.8%2.2 Scala - 0.6%, F# - 0.6%, Haskell - 0.5%
Bartosz Kwaśniewski Functional Programming in C++ 17 / 131
![Page 70: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/70.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language Features
C++ Language Features
Bartosz Kwaśniewski Functional Programming in C++ 18 / 131
![Page 71: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/71.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:
▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:
▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 72: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/72.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure
▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:
▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 73: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/73.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:
▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 74: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/74.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p
▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:
▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 75: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/75.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p
▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:
▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 76: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/76.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p
▶ C++17 - 1586p▶ Multiparadigm:
▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 77: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/77.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:
▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 78: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/78.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:
▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 79: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/79.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:▶ procedural
▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 80: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/80.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:▶ procedural▶ data abstraction
▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 81: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/81.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:▶ procedural▶ data abstraction▶ object oriented
▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 82: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/82.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:▶ procedural▶ data abstraction▶ object oriented▶ generic
▶ functional▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 83: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/83.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 84: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/84.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language
▶ C++ language is:▶ Imperative, eager, impure▶ Huge:
▶ C++98 - 879p▶ C++11 - 1324p▶ C++14 - 1366p▶ C++17 - 1586p
▶ Multiparadigm:▶ procedural▶ data abstraction▶ object oriented▶ generic▶ functional
▶ General purpose
Bartosz Kwaśniewski Functional Programming in C++ 19 / 131
![Page 85: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/85.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Computation Types I
▶ runtime computations//1. runtime computationsvector<int> v1{1,2,3};transform(v1.begin(), v1.end(), v1.begin(), [](int i){return i + 1;});
▶ constexpr computations//2. constexpr computationsconstexpr int factorial(int n) {
return n <= 1? 1 : (n * factorial(n - 1));}static_assert(factorial(4) == 24);
Bartosz Kwaśniewski Functional Programming in C++ 20 / 131
![Page 86: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/86.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Computation Types I
▶ runtime computations//1. runtime computationsvector<int> v1{1,2,3};transform(v1.begin(), v1.end(), v1.begin(), [](int i){return i + 1;});
▶ constexpr computations//2. constexpr computationsconstexpr int factorial(int n) {
return n <= 1? 1 : (n * factorial(n - 1));}static_assert(factorial(4) == 24);
Bartosz Kwaśniewski Functional Programming in C++ 20 / 131
![Page 87: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/87.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Computation Types II
▶ types computations//3. types computationsusing types = mpl::vector<int, char, float, void>;using pointers = mpl::transform<types, add_const_pointer<mpl::_1>>::type;
▶ heterogeneous computations//4. heterogeneous computationsfusion::vector<int, std::string, float> seq{1, "abc", 3.4f};fusion::vector<std::string, std::string, std::string> strings
= fusion::transform(seq, [](auto t) {std::stringstream ss;ss << t; return ss.str();});
Bartosz Kwaśniewski Functional Programming in C++ 21 / 131
![Page 88: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/88.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Computation Types II
▶ types computations//3. types computationsusing types = mpl::vector<int, char, float, void>;using pointers = mpl::transform<types, add_const_pointer<mpl::_1>>::type;
▶ heterogeneous computations//4. heterogeneous computationsfusion::vector<int, std::string, float> seq{1, "abc", 3.4f};fusion::vector<std::string, std::string, std::string> strings
= fusion::transform(seq, [](auto t) {std::stringstream ss;ss << t; return ss.str();});
Bartosz Kwaśniewski Functional Programming in C++ 21 / 131
![Page 89: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/89.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language Constructs▶ C++98
▶ function pointers, custom functors, autoptr, const,templates, partial_sum, accumulate, transform
▶ C++11
▶ auto, decltype, type aliases, move sematics, smartpointers, lambda expression, variadic templates,constant expressions, tuple
▶ C++14
▶ generic lambdas, generalized return type deduction,decltype(auto), generalized lambda captures, extendedconstexpr, type _t aliases, variable template
▶ C++17
▶ fold expressions, constexpr if, constexpr lambdaexpressions, class template deduction, optional, any,variant, reduce
Bartosz Kwaśniewski Functional Programming in C++ 22 / 131
![Page 90: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/90.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language Constructs▶ C++98
▶ function pointers, custom functors, autoptr, const,templates, partial_sum, accumulate, transform
▶ C++11
▶ auto, decltype, type aliases, move sematics, smartpointers, lambda expression, variadic templates,constant expressions, tuple
▶ C++14
▶ generic lambdas, generalized return type deduction,decltype(auto), generalized lambda captures, extendedconstexpr, type _t aliases, variable template
▶ C++17
▶ fold expressions, constexpr if, constexpr lambdaexpressions, class template deduction, optional, any,variant, reduce
Bartosz Kwaśniewski Functional Programming in C++ 22 / 131
![Page 91: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/91.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language Constructs▶ C++98
▶ function pointers, custom functors, autoptr, const,templates, partial_sum, accumulate, transform
▶ C++11
▶ auto, decltype, type aliases, move sematics, smartpointers, lambda expression, variadic templates,constant expressions, tuple
▶ C++14
▶ generic lambdas, generalized return type deduction,decltype(auto), generalized lambda captures, extendedconstexpr, type _t aliases, variable template
▶ C++17
▶ fold expressions, constexpr if, constexpr lambdaexpressions, class template deduction, optional, any,variant, reduce
Bartosz Kwaśniewski Functional Programming in C++ 22 / 131
![Page 92: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/92.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language Constructs▶ C++98
▶ function pointers, custom functors, autoptr, const,templates, partial_sum, accumulate, transform
▶ C++11▶ auto, decltype, type aliases, move sematics, smart
pointers, lambda expression, variadic templates,constant expressions, tuple
▶ C++14
▶ generic lambdas, generalized return type deduction,decltype(auto), generalized lambda captures, extendedconstexpr, type _t aliases, variable template
▶ C++17
▶ fold expressions, constexpr if, constexpr lambdaexpressions, class template deduction, optional, any,variant, reduce
Bartosz Kwaśniewski Functional Programming in C++ 22 / 131
![Page 93: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/93.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language Constructs▶ C++98
▶ function pointers, custom functors, autoptr, const,templates, partial_sum, accumulate, transform
▶ C++11▶ auto, decltype, type aliases, move sematics, smart
pointers, lambda expression, variadic templates,constant expressions, tuple
▶ C++14
▶ generic lambdas, generalized return type deduction,decltype(auto), generalized lambda captures, extendedconstexpr, type _t aliases, variable template
▶ C++17
▶ fold expressions, constexpr if, constexpr lambdaexpressions, class template deduction, optional, any,variant, reduce
Bartosz Kwaśniewski Functional Programming in C++ 22 / 131
![Page 94: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/94.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language Constructs▶ C++98
▶ function pointers, custom functors, autoptr, const,templates, partial_sum, accumulate, transform
▶ C++11▶ auto, decltype, type aliases, move sematics, smart
pointers, lambda expression, variadic templates,constant expressions, tuple
▶ C++14▶ generic lambdas, generalized return type deduction,
decltype(auto), generalized lambda captures, extendedconstexpr, type _t aliases, variable template
▶ C++17
▶ fold expressions, constexpr if, constexpr lambdaexpressions, class template deduction, optional, any,variant, reduce
Bartosz Kwaśniewski Functional Programming in C++ 22 / 131
![Page 95: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/95.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language Constructs▶ C++98
▶ function pointers, custom functors, autoptr, const,templates, partial_sum, accumulate, transform
▶ C++11▶ auto, decltype, type aliases, move sematics, smart
pointers, lambda expression, variadic templates,constant expressions, tuple
▶ C++14▶ generic lambdas, generalized return type deduction,
decltype(auto), generalized lambda captures, extendedconstexpr, type _t aliases, variable template
▶ C++17
▶ fold expressions, constexpr if, constexpr lambdaexpressions, class template deduction, optional, any,variant, reduce
Bartosz Kwaśniewski Functional Programming in C++ 22 / 131
![Page 96: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/96.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Language Constructs▶ C++98
▶ function pointers, custom functors, autoptr, const,templates, partial_sum, accumulate, transform
▶ C++11▶ auto, decltype, type aliases, move sematics, smart
pointers, lambda expression, variadic templates,constant expressions, tuple
▶ C++14▶ generic lambdas, generalized return type deduction,
decltype(auto), generalized lambda captures, extendedconstexpr, type _t aliases, variable template
▶ C++17▶ fold expressions, constexpr if, constexpr lambda
expressions, class template deduction, optional, any,variant, reduce
Bartosz Kwaśniewski Functional Programming in C++ 22 / 131
![Page 97: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/97.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Concepts
Functional Concepts
▶ What programming concepts define functionalparadigm?
▶ How does C++ implement those concepts?
Bartosz Kwaśniewski Functional Programming in C++ 23 / 131
![Page 98: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/98.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Concepts
Functional Concepts▶ What programming concepts define functionalparadigm?
▶ How does C++ implement those concepts?
Bartosz Kwaśniewski Functional Programming in C++ 23 / 131
![Page 99: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/99.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Concepts
Functional Concepts▶ What programming concepts define functionalparadigm?
▶ How does C++ implement those concepts?
Bartosz Kwaśniewski Functional Programming in C++ 23 / 131
![Page 100: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/100.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
FP Concepts and C++concept built-in substitution
first-class function - function pointer, functors, std::functionhigher-order function - function pointer, functors, std::function
pure function - const, move semantics, pass by valueanonymous function - lambda expressionnested function - nested classes, lambda expressionstype inference auto, decltype -
closures - lambda expressionsparial application - std::bind
currying - custom solutions with lambdas and templatesrecursion with tco sometimes -algebraic data types - compositionspattern matching - mach7lazy valuation - custom solution, suspension idiomimmutable data - custom solutions
polymorphic types - genericslist comprehension - custom solutions, range-v3
type classes and overloading - polymorhpismautomatic memory management semi, smart pointers -
monads opional, variant custom, librariescombinators - custom solutions, libraries
Bartosz Kwaśniewski Functional Programming in C++ 24 / 131
![Page 101: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/101.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Functional Programming Concepts
Bartosz Kwaśniewski Functional Programming in C++ 25 / 131
![Page 102: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/102.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Deterministic function: Haskell
▶ What is a deterministic function?
▶ It is a pure function (transformation)▶ A function without side effects▶ Result depends only on its arguments▶ Without involvement of any external or global states
fib :: Int -> Intfib 0 = 0fib 1 = 1fib n = fib (n - 1) + fib (n - 2)
Bartosz Kwaśniewski Functional Programming in C++ 26 / 131
![Page 103: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/103.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Deterministic function: Haskell
▶ What is a deterministic function?▶ It is a pure function (transformation)
▶ A function without side effects▶ Result depends only on its arguments▶ Without involvement of any external or global states
fib :: Int -> Intfib 0 = 0fib 1 = 1fib n = fib (n - 1) + fib (n - 2)
Bartosz Kwaśniewski Functional Programming in C++ 26 / 131
![Page 104: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/104.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Deterministic function: Haskell
▶ What is a deterministic function?▶ It is a pure function (transformation)▶ A function without side effects
▶ Result depends only on its arguments▶ Without involvement of any external or global states
fib :: Int -> Intfib 0 = 0fib 1 = 1fib n = fib (n - 1) + fib (n - 2)
Bartosz Kwaśniewski Functional Programming in C++ 26 / 131
![Page 105: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/105.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Deterministic function: Haskell
▶ What is a deterministic function?▶ It is a pure function (transformation)▶ A function without side effects▶ Result depends only on its arguments
▶ Without involvement of any external or global states
fib :: Int -> Intfib 0 = 0fib 1 = 1fib n = fib (n - 1) + fib (n - 2)
Bartosz Kwaśniewski Functional Programming in C++ 26 / 131
![Page 106: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/106.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Deterministic function: Haskell
▶ What is a deterministic function?▶ It is a pure function (transformation)▶ A function without side effects▶ Result depends only on its arguments▶ Without involvement of any external or global states
fib :: Int -> Intfib 0 = 0fib 1 = 1fib n = fib (n - 1) + fib (n - 2)
Bartosz Kwaśniewski Functional Programming in C++ 26 / 131
![Page 107: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/107.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Deterministic function: Haskell
▶ What is a deterministic function?▶ It is a pure function (transformation)▶ A function without side effects▶ Result depends only on its arguments▶ Without involvement of any external or global states
fib :: Int -> Intfib 0 = 0fib 1 = 1fib n = fib (n - 1) + fib (n - 2)
Bartosz Kwaśniewski Functional Programming in C++ 26 / 131
![Page 108: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/108.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pure Function: C++
▶ How are pure functions represented in C++?
▶ They aren’t, but...
▶ There are extensions:
▶ const (read and write not allowed from globals)▶ pure (read is allowed)
int sum1(int a, int b) __attribute__((const));int sum2(int a, int b) __attribute__((pure));
▶ Unfortunately ...
▶ Those are just hints for optimization▶ Purity is NOT checked or enforced
Bartosz Kwaśniewski Functional Programming in C++ 27 / 131
![Page 109: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/109.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pure Function: C++
▶ How are pure functions represented in C++?▶ They aren’t, but...
▶ There are extensions:
▶ const (read and write not allowed from globals)▶ pure (read is allowed)
int sum1(int a, int b) __attribute__((const));int sum2(int a, int b) __attribute__((pure));
▶ Unfortunately ...
▶ Those are just hints for optimization▶ Purity is NOT checked or enforced
Bartosz Kwaśniewski Functional Programming in C++ 27 / 131
![Page 110: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/110.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pure Function: C++
▶ How are pure functions represented in C++?▶ They aren’t, but...
▶ There are extensions:
▶ const (read and write not allowed from globals)▶ pure (read is allowed)
int sum1(int a, int b) __attribute__((const));int sum2(int a, int b) __attribute__((pure));
▶ Unfortunately ...
▶ Those are just hints for optimization▶ Purity is NOT checked or enforced
Bartosz Kwaśniewski Functional Programming in C++ 27 / 131
![Page 111: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/111.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pure Function: C++
▶ How are pure functions represented in C++?▶ They aren’t, but...
▶ There are extensions:▶ const (read and write not allowed from globals)▶ pure (read is allowed)
int sum1(int a, int b) __attribute__((const));int sum2(int a, int b) __attribute__((pure));
▶ Unfortunately ...
▶ Those are just hints for optimization▶ Purity is NOT checked or enforced
Bartosz Kwaśniewski Functional Programming in C++ 27 / 131
![Page 112: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/112.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pure Function: C++
▶ How are pure functions represented in C++?▶ They aren’t, but...
▶ There are extensions:▶ const (read and write not allowed from globals)▶ pure (read is allowed)
int sum1(int a, int b) __attribute__((const));int sum2(int a, int b) __attribute__((pure));
▶ Unfortunately ...
▶ Those are just hints for optimization▶ Purity is NOT checked or enforced
Bartosz Kwaśniewski Functional Programming in C++ 27 / 131
![Page 113: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/113.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pure Function: C++
▶ How are pure functions represented in C++?▶ They aren’t, but...
▶ There are extensions:▶ const (read and write not allowed from globals)▶ pure (read is allowed)
int sum1(int a, int b) __attribute__((const));int sum2(int a, int b) __attribute__((pure));
▶ Unfortunately ...
▶ Those are just hints for optimization▶ Purity is NOT checked or enforced
Bartosz Kwaśniewski Functional Programming in C++ 27 / 131
![Page 114: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/114.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pure Function: C++
▶ How are pure functions represented in C++?▶ They aren’t, but...
▶ There are extensions:▶ const (read and write not allowed from globals)▶ pure (read is allowed)
int sum1(int a, int b) __attribute__((const));int sum2(int a, int b) __attribute__((pure));
▶ Unfortunately ...▶ Those are just hints for optimization
▶ Purity is NOT checked or enforced
Bartosz Kwaśniewski Functional Programming in C++ 27 / 131
![Page 115: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/115.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pure Function: C++
▶ How are pure functions represented in C++?▶ They aren’t, but...
▶ There are extensions:▶ const (read and write not allowed from globals)▶ pure (read is allowed)
int sum1(int a, int b) __attribute__((const));int sum2(int a, int b) __attribute__((pure));
▶ Unfortunately ...▶ Those are just hints for optimization▶ Purity is NOT checked or enforced
Bartosz Kwaśniewski Functional Programming in C++ 27 / 131
![Page 116: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/116.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First-Class and Higher-Order function: Haskell
▶ What is a first-class function?
▶ Functions that could be used in all contexts as all otherentities:
▶ Create new functions from preexisting functions at run-time▶ Store functions in collections▶ Use functions as arguments to other functions▶ Use functions as return values of other functions
▶ What is a higher-order function?
▶ A function that takes another function as an argumentor returns a function
twice :: (a -> a) -> a -> atwice f x = f (f x)print(twice (+1) 2) -- prints 4
Bartosz Kwaśniewski Functional Programming in C++ 28 / 131
![Page 117: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/117.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First-Class and Higher-Order function: Haskell
▶ What is a first-class function?▶ Functions that could be used in all contexts as all other
entities:
▶ Create new functions from preexisting functions at run-time▶ Store functions in collections▶ Use functions as arguments to other functions▶ Use functions as return values of other functions
▶ What is a higher-order function?
▶ A function that takes another function as an argumentor returns a function
twice :: (a -> a) -> a -> atwice f x = f (f x)print(twice (+1) 2) -- prints 4
Bartosz Kwaśniewski Functional Programming in C++ 28 / 131
![Page 118: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/118.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First-Class and Higher-Order function: Haskell
▶ What is a first-class function?▶ Functions that could be used in all contexts as all other
entities:▶ Create new functions from preexisting functions at run-time
▶ Store functions in collections▶ Use functions as arguments to other functions▶ Use functions as return values of other functions
▶ What is a higher-order function?
▶ A function that takes another function as an argumentor returns a function
twice :: (a -> a) -> a -> atwice f x = f (f x)print(twice (+1) 2) -- prints 4
Bartosz Kwaśniewski Functional Programming in C++ 28 / 131
![Page 119: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/119.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First-Class and Higher-Order function: Haskell
▶ What is a first-class function?▶ Functions that could be used in all contexts as all other
entities:▶ Create new functions from preexisting functions at run-time▶ Store functions in collections
▶ Use functions as arguments to other functions▶ Use functions as return values of other functions
▶ What is a higher-order function?
▶ A function that takes another function as an argumentor returns a function
twice :: (a -> a) -> a -> atwice f x = f (f x)print(twice (+1) 2) -- prints 4
Bartosz Kwaśniewski Functional Programming in C++ 28 / 131
![Page 120: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/120.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First-Class and Higher-Order function: Haskell
▶ What is a first-class function?▶ Functions that could be used in all contexts as all other
entities:▶ Create new functions from preexisting functions at run-time▶ Store functions in collections▶ Use functions as arguments to other functions
▶ Use functions as return values of other functions
▶ What is a higher-order function?
▶ A function that takes another function as an argumentor returns a function
twice :: (a -> a) -> a -> atwice f x = f (f x)print(twice (+1) 2) -- prints 4
Bartosz Kwaśniewski Functional Programming in C++ 28 / 131
![Page 121: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/121.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First-Class and Higher-Order function: Haskell
▶ What is a first-class function?▶ Functions that could be used in all contexts as all other
entities:▶ Create new functions from preexisting functions at run-time▶ Store functions in collections▶ Use functions as arguments to other functions▶ Use functions as return values of other functions
▶ What is a higher-order function?
▶ A function that takes another function as an argumentor returns a function
twice :: (a -> a) -> a -> atwice f x = f (f x)print(twice (+1) 2) -- prints 4
Bartosz Kwaśniewski Functional Programming in C++ 28 / 131
![Page 122: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/122.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First-Class and Higher-Order function: Haskell
▶ What is a first-class function?▶ Functions that could be used in all contexts as all other
entities:▶ Create new functions from preexisting functions at run-time▶ Store functions in collections▶ Use functions as arguments to other functions▶ Use functions as return values of other functions
▶ What is a higher-order function?
▶ A function that takes another function as an argumentor returns a function
twice :: (a -> a) -> a -> atwice f x = f (f x)print(twice (+1) 2) -- prints 4
Bartosz Kwaśniewski Functional Programming in C++ 28 / 131
![Page 123: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/123.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First-Class and Higher-Order function: Haskell
▶ What is a first-class function?▶ Functions that could be used in all contexts as all other
entities:▶ Create new functions from preexisting functions at run-time▶ Store functions in collections▶ Use functions as arguments to other functions▶ Use functions as return values of other functions
▶ What is a higher-order function?▶ A function that takes another function as an argument
or returns a function
twice :: (a -> a) -> a -> atwice f x = f (f x)print(twice (+1) 2) -- prints 4
Bartosz Kwaśniewski Functional Programming in C++ 28 / 131
![Page 124: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/124.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First-Class and Higher-Order function: Haskell
▶ What is a first-class function?▶ Functions that could be used in all contexts as all other
entities:▶ Create new functions from preexisting functions at run-time▶ Store functions in collections▶ Use functions as arguments to other functions▶ Use functions as return values of other functions
▶ What is a higher-order function?▶ A function that takes another function as an argument
or returns a function
twice :: (a -> a) -> a -> atwice f x = f (f x)print(twice (+1) 2) -- prints 4
Bartosz Kwaśniewski Functional Programming in C++ 28 / 131
![Page 125: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/125.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First Class and Higher Order Functions: C++▶ Does C++ functions are first-class citizens andhigher-order?
▶ No, but... What are first-class entities in C++?▶ Yes, classes are.
▶ So there are:
▶ callable objects (std::function, custom)▶ lambda expressions
auto inc = [](int a){return a + 1;};auto twice = [](auto f, int a){return f(f(a));};cout << twice(inc, 2); //prints 4
auto hanatwice = hana::compose(inc,inc);cout << hanatwice(2);
▶ But what about function pointers?
▶ It is only the address without bounded variables
Bartosz Kwaśniewski Functional Programming in C++ 29 / 131
![Page 126: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/126.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First Class and Higher Order Functions: C++▶ Does C++ functions are first-class citizens andhigher-order?
▶ No, but... What are first-class entities in C++?
▶ Yes, classes are.▶ So there are:
▶ callable objects (std::function, custom)▶ lambda expressions
auto inc = [](int a){return a + 1;};auto twice = [](auto f, int a){return f(f(a));};cout << twice(inc, 2); //prints 4
auto hanatwice = hana::compose(inc,inc);cout << hanatwice(2);
▶ But what about function pointers?
▶ It is only the address without bounded variables
Bartosz Kwaśniewski Functional Programming in C++ 29 / 131
![Page 127: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/127.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First Class and Higher Order Functions: C++▶ Does C++ functions are first-class citizens andhigher-order?
▶ No, but... What are first-class entities in C++?▶ Yes, classes are.
▶ So there are:
▶ callable objects (std::function, custom)▶ lambda expressions
auto inc = [](int a){return a + 1;};auto twice = [](auto f, int a){return f(f(a));};cout << twice(inc, 2); //prints 4
auto hanatwice = hana::compose(inc,inc);cout << hanatwice(2);
▶ But what about function pointers?
▶ It is only the address without bounded variables
Bartosz Kwaśniewski Functional Programming in C++ 29 / 131
![Page 128: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/128.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First Class and Higher Order Functions: C++▶ Does C++ functions are first-class citizens andhigher-order?
▶ No, but... What are first-class entities in C++?▶ Yes, classes are.
▶ So there are:
▶ callable objects (std::function, custom)▶ lambda expressions
auto inc = [](int a){return a + 1;};auto twice = [](auto f, int a){return f(f(a));};cout << twice(inc, 2); //prints 4
auto hanatwice = hana::compose(inc,inc);cout << hanatwice(2);
▶ But what about function pointers?
▶ It is only the address without bounded variables
Bartosz Kwaśniewski Functional Programming in C++ 29 / 131
![Page 129: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/129.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First Class and Higher Order Functions: C++▶ Does C++ functions are first-class citizens andhigher-order?
▶ No, but... What are first-class entities in C++?▶ Yes, classes are.
▶ So there are:▶ callable objects (std::function, custom)
▶ lambda expressionsauto inc = [](int a){return a + 1;};auto twice = [](auto f, int a){return f(f(a));};cout << twice(inc, 2); //prints 4
auto hanatwice = hana::compose(inc,inc);cout << hanatwice(2);
▶ But what about function pointers?
▶ It is only the address without bounded variables
Bartosz Kwaśniewski Functional Programming in C++ 29 / 131
![Page 130: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/130.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First Class and Higher Order Functions: C++▶ Does C++ functions are first-class citizens andhigher-order?
▶ No, but... What are first-class entities in C++?▶ Yes, classes are.
▶ So there are:▶ callable objects (std::function, custom)▶ lambda expressions
auto inc = [](int a){return a + 1;};auto twice = [](auto f, int a){return f(f(a));};cout << twice(inc, 2); //prints 4
auto hanatwice = hana::compose(inc,inc);cout << hanatwice(2);
▶ But what about function pointers?
▶ It is only the address without bounded variables
Bartosz Kwaśniewski Functional Programming in C++ 29 / 131
![Page 131: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/131.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First Class and Higher Order Functions: C++▶ Does C++ functions are first-class citizens andhigher-order?
▶ No, but... What are first-class entities in C++?▶ Yes, classes are.
▶ So there are:▶ callable objects (std::function, custom)▶ lambda expressions
auto inc = [](int a){return a + 1;};auto twice = [](auto f, int a){return f(f(a));};cout << twice(inc, 2); //prints 4
auto hanatwice = hana::compose(inc,inc);cout << hanatwice(2);
▶ But what about function pointers?
▶ It is only the address without bounded variables
Bartosz Kwaśniewski Functional Programming in C++ 29 / 131
![Page 132: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/132.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First Class and Higher Order Functions: C++▶ Does C++ functions are first-class citizens andhigher-order?
▶ No, but... What are first-class entities in C++?▶ Yes, classes are.
▶ So there are:▶ callable objects (std::function, custom)▶ lambda expressions
auto inc = [](int a){return a + 1;};auto twice = [](auto f, int a){return f(f(a));};cout << twice(inc, 2); //prints 4
auto hanatwice = hana::compose(inc,inc);cout << hanatwice(2);
▶ But what about function pointers?
▶ It is only the address without bounded variables
Bartosz Kwaśniewski Functional Programming in C++ 29 / 131
![Page 133: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/133.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
First Class and Higher Order Functions: C++▶ Does C++ functions are first-class citizens andhigher-order?
▶ No, but... What are first-class entities in C++?▶ Yes, classes are.
▶ So there are:▶ callable objects (std::function, custom)▶ lambda expressions
auto inc = [](int a){return a + 1;};auto twice = [](auto f, int a){return f(f(a));};cout << twice(inc, 2); //prints 4
auto hanatwice = hana::compose(inc,inc);cout << hanatwice(2);
▶ But what about function pointers?▶ It is only the address without bounded variables
Bartosz Kwaśniewski Functional Programming in C++ 29 / 131
![Page 134: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/134.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Nested function: Haskell
▶ What is a nested function?
▶ A function defined within another function
nested :: Integer -> Integernested x = f 1 + f 2 where f y = x + yprint (nested 1) -- prints 5
Bartosz Kwaśniewski Functional Programming in C++ 30 / 131
![Page 135: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/135.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Nested function: Haskell
▶ What is a nested function?▶ A function defined within another function
nested :: Integer -> Integernested x = f 1 + f 2 where f y = x + yprint (nested 1) -- prints 5
Bartosz Kwaśniewski Functional Programming in C++ 30 / 131
![Page 136: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/136.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Nested function: Haskell
▶ What is a nested function?▶ A function defined within another function
nested :: Integer -> Integernested x = f 1 + f 2 where f y = x + yprint (nested 1) -- prints 5
Bartosz Kwaśniewski Functional Programming in C++ 30 / 131
![Page 137: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/137.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Nested Function: C++
▶ Does C++ support nested functions?
▶ No, but we have...▶ lambda expressions
auto nested = [](int x){auto f = [x](int y){return x + y;};return f(1) + f(2);
};
Bartosz Kwaśniewski Functional Programming in C++ 31 / 131
![Page 138: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/138.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Nested Function: C++
▶ Does C++ support nested functions?▶ No, but we have...
▶ lambda expressions
auto nested = [](int x){auto f = [x](int y){return x + y;};return f(1) + f(2);
};
Bartosz Kwaśniewski Functional Programming in C++ 31 / 131
![Page 139: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/139.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Nested Function: C++
▶ Does C++ support nested functions?▶ No, but we have...▶ lambda expressions
auto nested = [](int x){auto f = [x](int y){return x + y;};return f(1) + f(2);
};
Bartosz Kwaśniewski Functional Programming in C++ 31 / 131
![Page 140: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/140.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Nested Function: C++
▶ Does C++ support nested functions?▶ No, but we have...▶ lambda expressions
auto nested = [](int x){auto f = [x](int y){return x + y;};return f(1) + f(2);
};
Bartosz Kwaśniewski Functional Programming in C++ 31 / 131
![Page 141: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/141.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: Haskell
▶ What is an anonymous functions?
▶ It is a function definition that is not bound to anidentifier
▶ used in higher-order functions, as arguments or a resultthat needs to be returned
print ((\x y -> x + y) 1 2)
Bartosz Kwaśniewski Functional Programming in C++ 32 / 131
![Page 142: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/142.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: Haskell
▶ What is an anonymous functions?▶ It is a function definition that is not bound to an
identifier
▶ used in higher-order functions, as arguments or a resultthat needs to be returned
print ((\x y -> x + y) 1 2)
Bartosz Kwaśniewski Functional Programming in C++ 32 / 131
![Page 143: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/143.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: Haskell
▶ What is an anonymous functions?▶ It is a function definition that is not bound to an
identifier▶ used in higher-order functions, as arguments or a result
that needs to be returned
print ((\x y -> x + y) 1 2)
Bartosz Kwaśniewski Functional Programming in C++ 32 / 131
![Page 144: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/144.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: Haskell
▶ What is an anonymous functions?▶ It is a function definition that is not bound to an
identifier▶ used in higher-order functions, as arguments or a result
that needs to be returned
print ((\x y -> x + y) 1 2)
Bartosz Kwaśniewski Functional Programming in C++ 32 / 131
![Page 145: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/145.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: C++
▶ Does C++ support anonymous functions?
▶ No, but we have ...▶ lambda expressions.
cout << [](auto f){return [f](int a){
return f(a + 1);};
}([](int c){return c + 1;})(2)
▶ How many lambda expressions do we have here?
▶ Three anonymous lambda expressions
Bartosz Kwaśniewski Functional Programming in C++ 33 / 131
![Page 146: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/146.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: C++
▶ Does C++ support anonymous functions?▶ No, but we have ...
▶ lambda expressions.
cout << [](auto f){return [f](int a){
return f(a + 1);};
}([](int c){return c + 1;})(2)
▶ How many lambda expressions do we have here?
▶ Three anonymous lambda expressions
Bartosz Kwaśniewski Functional Programming in C++ 33 / 131
![Page 147: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/147.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: C++
▶ Does C++ support anonymous functions?▶ No, but we have ...▶ lambda expressions.
cout << [](auto f){return [f](int a){
return f(a + 1);};
}([](int c){return c + 1;})(2)
▶ How many lambda expressions do we have here?
▶ Three anonymous lambda expressions
Bartosz Kwaśniewski Functional Programming in C++ 33 / 131
![Page 148: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/148.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: C++
▶ Does C++ support anonymous functions?▶ No, but we have ...▶ lambda expressions.
cout << [](auto f){return [f](int a){
return f(a + 1);};
}([](int c){return c + 1;})(2)
▶ How many lambda expressions do we have here?
▶ Three anonymous lambda expressions
Bartosz Kwaśniewski Functional Programming in C++ 33 / 131
![Page 149: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/149.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: C++
▶ Does C++ support anonymous functions?▶ No, but we have ...▶ lambda expressions.
cout << [](auto f){return [f](int a){
return f(a + 1);};
}([](int c){return c + 1;})(2)
▶ How many lambda expressions do we have here?
▶ Three anonymous lambda expressions
Bartosz Kwaśniewski Functional Programming in C++ 33 / 131
![Page 150: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/150.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Anonymous Function: C++
▶ Does C++ support anonymous functions?▶ No, but we have ...▶ lambda expressions.
cout << [](auto f){return [f](int a){
return f(a + 1);};
}([](int c){return c + 1;})(2)
▶ How many lambda expressions do we have here?▶ Three anonymous lambda expressions
Bartosz Kwaśniewski Functional Programming in C++ 33 / 131
![Page 151: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/151.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: Haskell
▶ What is a closure?
▶ Instance of a function together with bindings to variablesused locally but defined in enclosing scope
▶ Callable object with all the captured variables
sum :: Num a => a -> (a -> a)sum y = \x -> x + y
Bartosz Kwaśniewski Functional Programming in C++ 34 / 131
![Page 152: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/152.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: Haskell
▶ What is a closure?▶ Instance of a function together with bindings to variables
used locally but defined in enclosing scope
▶ Callable object with all the captured variables
sum :: Num a => a -> (a -> a)sum y = \x -> x + y
Bartosz Kwaśniewski Functional Programming in C++ 34 / 131
![Page 153: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/153.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: Haskell
▶ What is a closure?▶ Instance of a function together with bindings to variables
used locally but defined in enclosing scope▶ Callable object with all the captured variables
sum :: Num a => a -> (a -> a)sum y = \x -> x + y
Bartosz Kwaśniewski Functional Programming in C++ 34 / 131
![Page 154: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/154.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: Haskell
▶ What is a closure?▶ Instance of a function together with bindings to variables
used locally but defined in enclosing scope▶ Callable object with all the captured variables
sum :: Num a => a -> (a -> a)sum y = \x -> x + y
Bartosz Kwaśniewski Functional Programming in C++ 34 / 131
![Page 155: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/155.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: C++
▶ Does C++ support closures?
▶ No, but we have...▶ lambda expressions.
▶ Closure terms related:
▶ Lambda expression: expression, part of a source code▶ Closure class: a functor class generated for each lambda
expression▶ Closure: object, instance of closure class
function<function<int(int)>(int, int)> closure = [](int a, int b) {return [a,b](int c){return a + b + c;};
};cout << closure(1,2)(3) << endl; //prints 6
Bartosz Kwaśniewski Functional Programming in C++ 35 / 131
![Page 156: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/156.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: C++
▶ Does C++ support closures?▶ No, but we have...
▶ lambda expressions.▶ Closure terms related:
▶ Lambda expression: expression, part of a source code▶ Closure class: a functor class generated for each lambda
expression▶ Closure: object, instance of closure class
function<function<int(int)>(int, int)> closure = [](int a, int b) {return [a,b](int c){return a + b + c;};
};cout << closure(1,2)(3) << endl; //prints 6
Bartosz Kwaśniewski Functional Programming in C++ 35 / 131
![Page 157: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/157.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: C++
▶ Does C++ support closures?▶ No, but we have...▶ lambda expressions.
▶ Closure terms related:
▶ Lambda expression: expression, part of a source code▶ Closure class: a functor class generated for each lambda
expression▶ Closure: object, instance of closure class
function<function<int(int)>(int, int)> closure = [](int a, int b) {return [a,b](int c){return a + b + c;};
};cout << closure(1,2)(3) << endl; //prints 6
Bartosz Kwaśniewski Functional Programming in C++ 35 / 131
![Page 158: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/158.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: C++
▶ Does C++ support closures?▶ No, but we have...▶ lambda expressions.
▶ Closure terms related:
▶ Lambda expression: expression, part of a source code▶ Closure class: a functor class generated for each lambda
expression▶ Closure: object, instance of closure class
function<function<int(int)>(int, int)> closure = [](int a, int b) {return [a,b](int c){return a + b + c;};
};cout << closure(1,2)(3) << endl; //prints 6
Bartosz Kwaśniewski Functional Programming in C++ 35 / 131
![Page 159: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/159.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: C++
▶ Does C++ support closures?▶ No, but we have...▶ lambda expressions.
▶ Closure terms related:▶ Lambda expression: expression, part of a source code
▶ Closure class: a functor class generated for each lambdaexpression
▶ Closure: object, instance of closure class
function<function<int(int)>(int, int)> closure = [](int a, int b) {return [a,b](int c){return a + b + c;};
};cout << closure(1,2)(3) << endl; //prints 6
Bartosz Kwaśniewski Functional Programming in C++ 35 / 131
![Page 160: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/160.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: C++
▶ Does C++ support closures?▶ No, but we have...▶ lambda expressions.
▶ Closure terms related:▶ Lambda expression: expression, part of a source code▶ Closure class: a functor class generated for each lambda
expression
▶ Closure: object, instance of closure class
function<function<int(int)>(int, int)> closure = [](int a, int b) {return [a,b](int c){return a + b + c;};
};cout << closure(1,2)(3) << endl; //prints 6
Bartosz Kwaśniewski Functional Programming in C++ 35 / 131
![Page 161: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/161.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Closure: C++
▶ Does C++ support closures?▶ No, but we have...▶ lambda expressions.
▶ Closure terms related:▶ Lambda expression: expression, part of a source code▶ Closure class: a functor class generated for each lambda
expression▶ Closure: object, instance of closure class
function<function<int(int)>(int, int)> closure = [](int a, int b) {return [a,b](int c){return a + b + c;};
};cout << closure(1,2)(3) << endl; //prints 6
Bartosz Kwaśniewski Functional Programming in C++ 35 / 131
![Page 162: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/162.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Partial application: Haskell
▶ What is a partial application?
▶ Supplying fewer than the total number of arguments▶ Process of creating a new function with lower arity by
fixing a number of arguments to a function
partialSum :: Num a => a -> a -> a -> apartialSum a b c = a + b + cpartialSum1 = partialSum 1partialSum2 = partialSum1 2print (partialSum2 3) -- prints 6
Bartosz Kwaśniewski Functional Programming in C++ 36 / 131
![Page 163: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/163.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Partial application: Haskell
▶ What is a partial application?▶ Supplying fewer than the total number of arguments
▶ Process of creating a new function with lower arity byfixing a number of arguments to a function
partialSum :: Num a => a -> a -> a -> apartialSum a b c = a + b + cpartialSum1 = partialSum 1partialSum2 = partialSum1 2print (partialSum2 3) -- prints 6
Bartosz Kwaśniewski Functional Programming in C++ 36 / 131
![Page 164: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/164.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Partial application: Haskell
▶ What is a partial application?▶ Supplying fewer than the total number of arguments▶ Process of creating a new function with lower arity by
fixing a number of arguments to a function
partialSum :: Num a => a -> a -> a -> apartialSum a b c = a + b + cpartialSum1 = partialSum 1partialSum2 = partialSum1 2print (partialSum2 3) -- prints 6
Bartosz Kwaśniewski Functional Programming in C++ 36 / 131
![Page 165: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/165.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Partial application: Haskell
▶ What is a partial application?▶ Supplying fewer than the total number of arguments▶ Process of creating a new function with lower arity by
fixing a number of arguments to a function
partialSum :: Num a => a -> a -> a -> apartialSum a b c = a + b + cpartialSum1 = partialSum 1partialSum2 = partialSum1 2print (partialSum2 3) -- prints 6
Bartosz Kwaśniewski Functional Programming in C++ 36 / 131
![Page 166: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/166.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Partial Application: C++▶ Does C++ support partial application?
▶ No, but we have...▶ bind and boost::hana library.
int addabc(int a, int b, int c) {return a + b + c;
}//cout << addabc(1,2)(3);//error: no matching function for call to addabc//note: candidate function not viable: requires 3 args, but 2 were provided
cout << bind(addabc, 1, 2, _1)(3);cout << hana::partial(addabc, 1, 2)(3);
cout << bind(addabc, 1, _1, _2)(2,3);cout << hana::partial(addabc, 1)(2,3);
cout << bind(bind(addabc, 1, _1, _2), 2, _1)(3);cout << hana::partial(hana::partial(addabc, 1),2)(3);
Bartosz Kwaśniewski Functional Programming in C++ 37 / 131
![Page 167: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/167.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Partial Application: C++▶ Does C++ support partial application?
▶ No, but we have...
▶ bind and boost::hana library.
int addabc(int a, int b, int c) {return a + b + c;
}//cout << addabc(1,2)(3);//error: no matching function for call to addabc//note: candidate function not viable: requires 3 args, but 2 were provided
cout << bind(addabc, 1, 2, _1)(3);cout << hana::partial(addabc, 1, 2)(3);
cout << bind(addabc, 1, _1, _2)(2,3);cout << hana::partial(addabc, 1)(2,3);
cout << bind(bind(addabc, 1, _1, _2), 2, _1)(3);cout << hana::partial(hana::partial(addabc, 1),2)(3);
Bartosz Kwaśniewski Functional Programming in C++ 37 / 131
![Page 168: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/168.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Partial Application: C++▶ Does C++ support partial application?
▶ No, but we have...▶ bind and boost::hana library.
int addabc(int a, int b, int c) {return a + b + c;
}//cout << addabc(1,2)(3);//error: no matching function for call to addabc//note: candidate function not viable: requires 3 args, but 2 were provided
cout << bind(addabc, 1, 2, _1)(3);cout << hana::partial(addabc, 1, 2)(3);
cout << bind(addabc, 1, _1, _2)(2,3);cout << hana::partial(addabc, 1)(2,3);
cout << bind(bind(addabc, 1, _1, _2), 2, _1)(3);cout << hana::partial(hana::partial(addabc, 1),2)(3);
Bartosz Kwaśniewski Functional Programming in C++ 37 / 131
![Page 169: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/169.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Partial Application: C++▶ Does C++ support partial application?
▶ No, but we have...▶ bind and boost::hana library.
int addabc(int a, int b, int c) {return a + b + c;
}//cout << addabc(1,2)(3);//error: no matching function for call to addabc//note: candidate function not viable: requires 3 args, but 2 were provided
cout << bind(addabc, 1, 2, _1)(3);cout << hana::partial(addabc, 1, 2)(3);
cout << bind(addabc, 1, _1, _2)(2,3);cout << hana::partial(addabc, 1)(2,3);
cout << bind(bind(addabc, 1, _1, _2), 2, _1)(3);cout << hana::partial(hana::partial(addabc, 1),2)(3);
Bartosz Kwaśniewski Functional Programming in C++ 37 / 131
![Page 170: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/170.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Currying: Haskell
▶ What is a currying application?
▶ It is a translation of a one multi-argument function intoa chain of multi one argument functions
▶ In haskell all functions are one argument functions
--int to function--f :: Int -> (Int -> Int)f :: Int -> Int -> Intf a b = a + b
--function to intg :: (Int, Int) -> Intg (a, b) = a + b
--Bidirectional transformation:print (f 1 2 + g(1,2) + (curry g) 1 2 + (uncurry f)(1,2))
Bartosz Kwaśniewski Functional Programming in C++ 38 / 131
![Page 171: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/171.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Currying: Haskell
▶ What is a currying application?▶ It is a translation of a one multi-argument function into
a chain of multi one argument functions
▶ In haskell all functions are one argument functions
--int to function--f :: Int -> (Int -> Int)f :: Int -> Int -> Intf a b = a + b
--function to intg :: (Int, Int) -> Intg (a, b) = a + b
--Bidirectional transformation:print (f 1 2 + g(1,2) + (curry g) 1 2 + (uncurry f)(1,2))
Bartosz Kwaśniewski Functional Programming in C++ 38 / 131
![Page 172: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/172.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Currying: Haskell
▶ What is a currying application?▶ It is a translation of a one multi-argument function into
a chain of multi one argument functions▶ In haskell all functions are one argument functions
--int to function--f :: Int -> (Int -> Int)f :: Int -> Int -> Intf a b = a + b
--function to intg :: (Int, Int) -> Intg (a, b) = a + b
--Bidirectional transformation:print (f 1 2 + g(1,2) + (curry g) 1 2 + (uncurry f)(1,2))
Bartosz Kwaśniewski Functional Programming in C++ 38 / 131
![Page 173: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/173.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Currying: Haskell
▶ What is a currying application?▶ It is a translation of a one multi-argument function into
a chain of multi one argument functions▶ In haskell all functions are one argument functions
--int to function--f :: Int -> (Int -> Int)f :: Int -> Int -> Intf a b = a + b
--function to intg :: (Int, Int) -> Intg (a, b) = a + b
--Bidirectional transformation:print (f 1 2 + g(1,2) + (curry g) 1 2 + (uncurry f)(1,2))
Bartosz Kwaśniewski Functional Programming in C++ 38 / 131
![Page 174: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/174.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Currying: C++
▶ Does C++ support currying?
▶ No, but we have...▶ templates and boost::hana library
int addabc(int a, int b, int c) {return a + b + c;
}
cout << hana::curry<3>(addabc)(1)(2)(3) << endl;cout << hana::curry<3>(addabc)(1)(2,3) << endl;cout << hana::curry<3>(addabc)(1,2,3) << endl;
Bartosz Kwaśniewski Functional Programming in C++ 39 / 131
![Page 175: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/175.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Currying: C++
▶ Does C++ support currying?▶ No, but we have...
▶ templates and boost::hana library
int addabc(int a, int b, int c) {return a + b + c;
}
cout << hana::curry<3>(addabc)(1)(2)(3) << endl;cout << hana::curry<3>(addabc)(1)(2,3) << endl;cout << hana::curry<3>(addabc)(1,2,3) << endl;
Bartosz Kwaśniewski Functional Programming in C++ 39 / 131
![Page 176: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/176.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Currying: C++
▶ Does C++ support currying?▶ No, but we have...▶ templates and boost::hana library
int addabc(int a, int b, int c) {return a + b + c;
}
cout << hana::curry<3>(addabc)(1)(2)(3) << endl;cout << hana::curry<3>(addabc)(1)(2,3) << endl;cout << hana::curry<3>(addabc)(1,2,3) << endl;
Bartosz Kwaśniewski Functional Programming in C++ 39 / 131
![Page 177: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/177.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Currying: C++
▶ Does C++ support currying?▶ No, but we have...▶ templates and boost::hana library
int addabc(int a, int b, int c) {return a + b + c;
}
cout << hana::curry<3>(addabc)(1)(2)(3) << endl;cout << hana::curry<3>(addabc)(1)(2,3) << endl;cout << hana::curry<3>(addabc)(1,2,3) << endl;
Bartosz Kwaśniewski Functional Programming in C++ 39 / 131
![Page 178: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/178.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursive Function and TCO: Haskell
▶ Are there loops in functional languages?
▶ No, the recursive functions act like loops▶ What is a recursive function
▶ Ok, that was a rhetoric question
▶ When Tail Call Optimization happened?
▶ When there is only a single recursive call and this call islast statement in the function
▶ TCO reuses the same call stack, and TCO is critical infunctional languages
▶ No jump-and-add-stack-frame immediately followed by apop-stack-frame-and-return-to-caller
▶ But simply jump to the start of the second routine (goto)
Bartosz Kwaśniewski Functional Programming in C++ 40 / 131
![Page 179: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/179.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursive Function and TCO: Haskell
▶ Are there loops in functional languages?▶ No, the recursive functions act like loops
▶ What is a recursive function
▶ Ok, that was a rhetoric question
▶ When Tail Call Optimization happened?
▶ When there is only a single recursive call and this call islast statement in the function
▶ TCO reuses the same call stack, and TCO is critical infunctional languages
▶ No jump-and-add-stack-frame immediately followed by apop-stack-frame-and-return-to-caller
▶ But simply jump to the start of the second routine (goto)
Bartosz Kwaśniewski Functional Programming in C++ 40 / 131
![Page 180: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/180.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursive Function and TCO: Haskell
▶ Are there loops in functional languages?▶ No, the recursive functions act like loops
▶ What is a recursive function
▶ Ok, that was a rhetoric question▶ When Tail Call Optimization happened?
▶ When there is only a single recursive call and this call islast statement in the function
▶ TCO reuses the same call stack, and TCO is critical infunctional languages
▶ No jump-and-add-stack-frame immediately followed by apop-stack-frame-and-return-to-caller
▶ But simply jump to the start of the second routine (goto)
Bartosz Kwaśniewski Functional Programming in C++ 40 / 131
![Page 181: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/181.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursive Function and TCO: Haskell
▶ Are there loops in functional languages?▶ No, the recursive functions act like loops
▶ What is a recursive function▶ Ok, that was a rhetoric question
▶ When Tail Call Optimization happened?
▶ When there is only a single recursive call and this call islast statement in the function
▶ TCO reuses the same call stack, and TCO is critical infunctional languages
▶ No jump-and-add-stack-frame immediately followed by apop-stack-frame-and-return-to-caller
▶ But simply jump to the start of the second routine (goto)
Bartosz Kwaśniewski Functional Programming in C++ 40 / 131
![Page 182: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/182.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursive Function and TCO: Haskell
▶ Are there loops in functional languages?▶ No, the recursive functions act like loops
▶ What is a recursive function▶ Ok, that was a rhetoric question
▶ When Tail Call Optimization happened?
▶ When there is only a single recursive call and this call islast statement in the function
▶ TCO reuses the same call stack, and TCO is critical infunctional languages
▶ No jump-and-add-stack-frame immediately followed by apop-stack-frame-and-return-to-caller
▶ But simply jump to the start of the second routine (goto)
Bartosz Kwaśniewski Functional Programming in C++ 40 / 131
![Page 183: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/183.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursive Function and TCO: Haskell
▶ Are there loops in functional languages?▶ No, the recursive functions act like loops
▶ What is a recursive function▶ Ok, that was a rhetoric question
▶ When Tail Call Optimization happened?▶ When there is only a single recursive call and this call is
last statement in the function
▶ TCO reuses the same call stack, and TCO is critical infunctional languages
▶ No jump-and-add-stack-frame immediately followed by apop-stack-frame-and-return-to-caller
▶ But simply jump to the start of the second routine (goto)
Bartosz Kwaśniewski Functional Programming in C++ 40 / 131
![Page 184: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/184.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursive Function and TCO: Haskell
▶ Are there loops in functional languages?▶ No, the recursive functions act like loops
▶ What is a recursive function▶ Ok, that was a rhetoric question
▶ When Tail Call Optimization happened?▶ When there is only a single recursive call and this call is
last statement in the function▶ TCO reuses the same call stack, and TCO is critical in
functional languages
▶ No jump-and-add-stack-frame immediately followed by apop-stack-frame-and-return-to-caller
▶ But simply jump to the start of the second routine (goto)
Bartosz Kwaśniewski Functional Programming in C++ 40 / 131
![Page 185: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/185.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursive Function and TCO: Haskell
▶ Are there loops in functional languages?▶ No, the recursive functions act like loops
▶ What is a recursive function▶ Ok, that was a rhetoric question
▶ When Tail Call Optimization happened?▶ When there is only a single recursive call and this call is
last statement in the function▶ TCO reuses the same call stack, and TCO is critical in
functional languages▶ No jump-and-add-stack-frame immediately followed by a
pop-stack-frame-and-return-to-caller
▶ But simply jump to the start of the second routine (goto)
Bartosz Kwaśniewski Functional Programming in C++ 40 / 131
![Page 186: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/186.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursive Function and TCO: Haskell
▶ Are there loops in functional languages?▶ No, the recursive functions act like loops
▶ What is a recursive function▶ Ok, that was a rhetoric question
▶ When Tail Call Optimization happened?▶ When there is only a single recursive call and this call is
last statement in the function▶ TCO reuses the same call stack, and TCO is critical in
functional languages▶ No jump-and-add-stack-frame immediately followed by a
pop-stack-frame-and-return-to-caller▶ But simply jump to the start of the second routine (goto)
Bartosz Kwaśniewski Functional Programming in C++ 40 / 131
![Page 187: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/187.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Recursion: Factorial Implementations
-- Junior Haskell programmer:factorial 0 = 1factorial n = n * factorial (n - 1)
-- Another junior haskell developer (tail call optimization)factorial x = fac' x 1 where
factorial' 1 y = yfactorial' x y = factorial' (x-1) (x*y)
-- Senior haskell developer (TCO with strictness)factorial x = fac' x 1 where
factorial' 1 y = yfactorial' x y = factorial' (x-1) $! (x*y)
-- Tenured professor (teaching Haskell to freshmen)factorial n = product [1..n]
Bartosz Kwaśniewski Functional Programming in C++ 41 / 131
![Page 188: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/188.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Tail Call Optimization: C++
▶ Does C++ support TCO?
▶ No, Tail Call Optimisation isn’t in the C++ standard▶ All modern compilers support it (O2)▶ Difficult to implement
int factco_times (int n, int acc) {if (n == 0) return acc;else return factco_times(n - 1, acc * n);
}
int factco (int n) {return factco_times (n, 1);
}
cout << factco(5) << endl; //prints 120
Bartosz Kwaśniewski Functional Programming in C++ 42 / 131
![Page 189: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/189.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Tail Call Optimization: C++
▶ Does C++ support TCO?▶ No, Tail Call Optimisation isn’t in the C++ standard
▶ All modern compilers support it (O2)▶ Difficult to implement
int factco_times (int n, int acc) {if (n == 0) return acc;else return factco_times(n - 1, acc * n);
}
int factco (int n) {return factco_times (n, 1);
}
cout << factco(5) << endl; //prints 120
Bartosz Kwaśniewski Functional Programming in C++ 42 / 131
![Page 190: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/190.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Tail Call Optimization: C++
▶ Does C++ support TCO?▶ No, Tail Call Optimisation isn’t in the C++ standard▶ All modern compilers support it (O2)
▶ Difficult to implement
int factco_times (int n, int acc) {if (n == 0) return acc;else return factco_times(n - 1, acc * n);
}
int factco (int n) {return factco_times (n, 1);
}
cout << factco(5) << endl; //prints 120
Bartosz Kwaśniewski Functional Programming in C++ 42 / 131
![Page 191: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/191.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Tail Call Optimization: C++
▶ Does C++ support TCO?▶ No, Tail Call Optimisation isn’t in the C++ standard▶ All modern compilers support it (O2)▶ Difficult to implement
int factco_times (int n, int acc) {if (n == 0) return acc;else return factco_times(n - 1, acc * n);
}
int factco (int n) {return factco_times (n, 1);
}
cout << factco(5) << endl; //prints 120
Bartosz Kwaśniewski Functional Programming in C++ 42 / 131
![Page 192: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/192.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Tail Call Optimization: C++
▶ Does C++ support TCO?▶ No, Tail Call Optimisation isn’t in the C++ standard▶ All modern compilers support it (O2)▶ Difficult to implement
int factco_times (int n, int acc) {if (n == 0) return acc;else return factco_times(n - 1, acc * n);
}
int factco (int n) {return factco_times (n, 1);
}
cout << factco(5) << endl; //prints 120
Bartosz Kwaśniewski Functional Programming in C++ 42 / 131
![Page 193: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/193.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Types System: Haskell and C++
▶ What type system is important in FP?
▶ Only static and strong typy system?▶ Why? Why not weak and dynamic?
▶ Do we want to make monkeys happy, or do we want toproduce correct programs?
▶ Types Are About Composability▶ Type checking provides barrier against nonsensical
programs▶ This is what Haskell and C++ have▶ Strong and static type system with type inference
Bartosz Kwaśniewski Functional Programming in C++ 43 / 131
![Page 194: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/194.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Types System: Haskell and C++
▶ What type system is important in FP?▶ Only static and strong typy system?
▶ Why? Why not weak and dynamic?
▶ Do we want to make monkeys happy, or do we want toproduce correct programs?
▶ Types Are About Composability▶ Type checking provides barrier against nonsensical
programs▶ This is what Haskell and C++ have▶ Strong and static type system with type inference
Bartosz Kwaśniewski Functional Programming in C++ 43 / 131
![Page 195: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/195.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Types System: Haskell and C++
▶ What type system is important in FP?▶ Only static and strong typy system?
▶ Why? Why not weak and dynamic?
▶ Do we want to make monkeys happy, or do we want toproduce correct programs?
▶ Types Are About Composability▶ Type checking provides barrier against nonsensical
programs▶ This is what Haskell and C++ have▶ Strong and static type system with type inference
Bartosz Kwaśniewski Functional Programming in C++ 43 / 131
![Page 196: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/196.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Types System: Haskell and C++
▶ What type system is important in FP?▶ Only static and strong typy system?
▶ Why? Why not weak and dynamic?▶ Do we want to make monkeys happy, or do we want to
produce correct programs?
▶ Types Are About Composability▶ Type checking provides barrier against nonsensical
programs▶ This is what Haskell and C++ have▶ Strong and static type system with type inference
Bartosz Kwaśniewski Functional Programming in C++ 43 / 131
![Page 197: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/197.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Types System: Haskell and C++
▶ What type system is important in FP?▶ Only static and strong typy system?
▶ Why? Why not weak and dynamic?▶ Do we want to make monkeys happy, or do we want to
produce correct programs?▶ Types Are About Composability
▶ Type checking provides barrier against nonsensicalprograms
▶ This is what Haskell and C++ have▶ Strong and static type system with type inference
Bartosz Kwaśniewski Functional Programming in C++ 43 / 131
![Page 198: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/198.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Types System: Haskell and C++
▶ What type system is important in FP?▶ Only static and strong typy system?
▶ Why? Why not weak and dynamic?▶ Do we want to make monkeys happy, or do we want to
produce correct programs?▶ Types Are About Composability▶ Type checking provides barrier against nonsensical
programs
▶ This is what Haskell and C++ have▶ Strong and static type system with type inference
Bartosz Kwaśniewski Functional Programming in C++ 43 / 131
![Page 199: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/199.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Types System: Haskell and C++
▶ What type system is important in FP?▶ Only static and strong typy system?
▶ Why? Why not weak and dynamic?▶ Do we want to make monkeys happy, or do we want to
produce correct programs?▶ Types Are About Composability▶ Type checking provides barrier against nonsensical
programs▶ This is what Haskell and C++ have
▶ Strong and static type system with type inference
Bartosz Kwaśniewski Functional Programming in C++ 43 / 131
![Page 200: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/200.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Types System: Haskell and C++
▶ What type system is important in FP?▶ Only static and strong typy system?
▶ Why? Why not weak and dynamic?▶ Do we want to make monkeys happy, or do we want to
produce correct programs?▶ Types Are About Composability▶ Type checking provides barrier against nonsensical
programs▶ This is what Haskell and C++ have▶ Strong and static type system with type inference
Bartosz Kwaśniewski Functional Programming in C++ 43 / 131
![Page 201: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/201.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: Haskell
▶ What is a type inference?
▶ Automatic deduction of the data type of an expression▶ Based on value of expression
Prelude> let sum a b = a + bPrelude> :t sumsum :: Num a => a -> a -> a
Bartosz Kwaśniewski Functional Programming in C++ 44 / 131
![Page 202: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/202.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: Haskell
▶ What is a type inference?▶ Automatic deduction of the data type of an expression
▶ Based on value of expression
Prelude> let sum a b = a + bPrelude> :t sumsum :: Num a => a -> a -> a
Bartosz Kwaśniewski Functional Programming in C++ 44 / 131
![Page 203: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/203.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: Haskell
▶ What is a type inference?▶ Automatic deduction of the data type of an expression▶ Based on value of expression
Prelude> let sum a b = a + bPrelude> :t sumsum :: Num a => a -> a -> a
Bartosz Kwaśniewski Functional Programming in C++ 44 / 131
![Page 204: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/204.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: Haskell
▶ What is a type inference?▶ Automatic deduction of the data type of an expression▶ Based on value of expression
Prelude> let sum a b = a + bPrelude> :t sumsum :: Num a => a -> a -> a
Bartosz Kwaśniewski Functional Programming in C++ 44 / 131
![Page 205: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/205.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: C++
▶ Does C++ support type inference?
▶ And...▶ Yes
▶ C++ Features related to type inference
▶ auto keyword, deducing the type of variable from itsinitializer expression
▶ decltype(auto) and return type deduction for normalfunctions
▶ Alternate type deduction when declaring a variable▶ Type deduction for lambda arguments
Bartosz Kwaśniewski Functional Programming in C++ 45 / 131
![Page 206: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/206.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: C++
▶ Does C++ support type inference?▶ And...
▶ Yes▶ C++ Features related to type inference
▶ auto keyword, deducing the type of variable from itsinitializer expression
▶ decltype(auto) and return type deduction for normalfunctions
▶ Alternate type deduction when declaring a variable▶ Type deduction for lambda arguments
Bartosz Kwaśniewski Functional Programming in C++ 45 / 131
![Page 207: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/207.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: C++
▶ Does C++ support type inference?▶ And...▶ Yes
▶ C++ Features related to type inference
▶ auto keyword, deducing the type of variable from itsinitializer expression
▶ decltype(auto) and return type deduction for normalfunctions
▶ Alternate type deduction when declaring a variable▶ Type deduction for lambda arguments
Bartosz Kwaśniewski Functional Programming in C++ 45 / 131
![Page 208: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/208.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: C++
▶ Does C++ support type inference?▶ And...▶ Yes
▶ C++ Features related to type inference
▶ auto keyword, deducing the type of variable from itsinitializer expression
▶ decltype(auto) and return type deduction for normalfunctions
▶ Alternate type deduction when declaring a variable▶ Type deduction for lambda arguments
Bartosz Kwaśniewski Functional Programming in C++ 45 / 131
![Page 209: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/209.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: C++
▶ Does C++ support type inference?▶ And...▶ Yes
▶ C++ Features related to type inference▶ auto keyword, deducing the type of variable from its
initializer expression
▶ decltype(auto) and return type deduction for normalfunctions
▶ Alternate type deduction when declaring a variable▶ Type deduction for lambda arguments
Bartosz Kwaśniewski Functional Programming in C++ 45 / 131
![Page 210: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/210.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: C++
▶ Does C++ support type inference?▶ And...▶ Yes
▶ C++ Features related to type inference▶ auto keyword, deducing the type of variable from its
initializer expression▶ decltype(auto) and return type deduction for normal
functions
▶ Alternate type deduction when declaring a variable▶ Type deduction for lambda arguments
Bartosz Kwaśniewski Functional Programming in C++ 45 / 131
![Page 211: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/211.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: C++
▶ Does C++ support type inference?▶ And...▶ Yes
▶ C++ Features related to type inference▶ auto keyword, deducing the type of variable from its
initializer expression▶ decltype(auto) and return type deduction for normal
functions▶ Alternate type deduction when declaring a variable
▶ Type deduction for lambda arguments
Bartosz Kwaśniewski Functional Programming in C++ 45 / 131
![Page 212: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/212.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: C++
▶ Does C++ support type inference?▶ And...▶ Yes
▶ C++ Features related to type inference▶ auto keyword, deducing the type of variable from its
initializer expression▶ decltype(auto) and return type deduction for normal
functions▶ Alternate type deduction when declaring a variable▶ Type deduction for lambda arguments
Bartosz Kwaśniewski Functional Programming in C++ 45 / 131
![Page 213: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/213.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Inference: C++
//decltype(auto) and return type deduction for normal functionsauto f() { return 42; } // return type is inttemplate<typename T, typename U>auto add(T t, U u) -> decltype(t + u); //return type is operator+(T,U)
//Alternate type deduction when declaring a variableint i;auto x4a = (i); // decltype(x4a) is intdecltype(auto) x4d = (i); // decltype(x4d) is int&
//Type deduction for lambda arguments[](auto x, auto y) {return x + y;};
Bartosz Kwaśniewski Functional Programming in C++ 46 / 131
![Page 214: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/214.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: Haskell
▶ What is a lazy evaluation?
▶ Evaluation at a time of need not at a time of a definition▶ Only when value is demanded
lazy n = take n (filter odd [1..])print (lazy 5) -- prints 1,3,5,7,9
Bartosz Kwaśniewski Functional Programming in C++ 47 / 131
![Page 215: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/215.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: Haskell
▶ What is a lazy evaluation?▶ Evaluation at a time of need not at a time of a definition
▶ Only when value is demanded
lazy n = take n (filter odd [1..])print (lazy 5) -- prints 1,3,5,7,9
Bartosz Kwaśniewski Functional Programming in C++ 47 / 131
![Page 216: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/216.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: Haskell
▶ What is a lazy evaluation?▶ Evaluation at a time of need not at a time of a definition▶ Only when value is demanded
lazy n = take n (filter odd [1..])print (lazy 5) -- prints 1,3,5,7,9
Bartosz Kwaśniewski Functional Programming in C++ 47 / 131
![Page 217: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/217.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: Haskell
▶ What is a lazy evaluation?▶ Evaluation at a time of need not at a time of a definition▶ Only when value is demanded
lazy n = take n (filter odd [1..])print (lazy 5) -- prints 1,3,5,7,9
Bartosz Kwaśniewski Functional Programming in C++ 47 / 131
![Page 218: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/218.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library
▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky
▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 219: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/219.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.
▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library
▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky
▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 220: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/220.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky
▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 221: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/221.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky
▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 222: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/222.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library
▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky
▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 223: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/223.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky
▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 224: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/224.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library▶ Why laziness is important?
▶ Inverts the flow of control
▶ Makes the code more reusable▶ But, laziness is tricky
▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 225: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/225.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky
▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 226: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/226.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky
▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 227: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/227.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky▶ Harder to reason about
▶ Performance bottleneck of exhaustive memorydeallocations
Bartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 228: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/228.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation▶ Does C++ support lazy evaluation?
▶ No, C++ is inherently eager.▶ But... there are:
▶ Custom solutions based on: suspensions, memoizationwith thunks, infinite lists, streams with cels combinators.
▶ Hana and Range v3 library▶ Why laziness is important?
▶ Inverts the flow of control▶ Makes the code more reusable
▶ But, laziness is tricky▶ Harder to reason about▶ Performance bottleneck of exhaustive memory
deallocationsBartosz Kwaśniewski Functional Programming in C++ 48 / 131
![Page 229: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/229.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: Haskell and C++
▶ Haskell (All)
triples = [(x, y, z) | z<-[1..], x<-[1..z], y<-[x..z], x^2 + y^2 == z^2]main = print (take 10 triples)
▶ C++ (printing part)
auto strm = triples().take(10);forEach(move(strm), [](tuple<int, int, int> const & t) {
cout << get<0>(t) << ", " << get<1>(t) << ", "<< get<2>(t) << endl;});
Bartosz Kwaśniewski Functional Programming in C++ 49 / 131
![Page 230: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/230.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Triple)
Stream<std::tuple<int, int, int>> triples(){
return mbind(intsFrom(1), [](int z){
return mbind(ints(1, z), [z](int x){
return mbind(ints(x, z), [x, z](int y){
return mthen(guard(x*x + y*y == z*z), [x, y, z](){
return mreturn(std::make_tuple(x, y, z));});
});});
});}
Bartosz Kwaśniewski Functional Programming in C++ 50 / 131
![Page 231: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/231.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Functions)
Stream<void*> guard(bool b){
if (b) return Stream<void*>(nullptr);else return Stream<void*>();
}
template<class T>Stream<T> mreturn(T v){
return Stream<T>([v]() {return Cell<T>(v);
});}
template<class T, class F>auto mthen(Stream<T> stm, F f) -> decltype(f()){
return mjoin(fmapv(stm, f));}
Bartosz Kwaśniewski Functional Programming in C++ 51 / 131
![Page 232: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/232.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Functions)
template<class T, class F>auto fmapv(Stream<T> stm, F f)->Stream<decltype(f())>{
using U = decltype(f());static_assert(std::is_convertible<F, std::function<U()>>::value,
"fmapv requires a function type U()");
if (stm.isEmpty()) return Stream<U>();return Stream<U>([stm, f](){
return Cell<U>(f(), fmapv(stm.pop_front(), f));});
}
Bartosz Kwaśniewski Functional Programming in C++ 52 / 131
![Page 233: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/233.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Functions)
template<class T, class F>auto mbind(Stream<T> stm, F f) -> decltype(f(stm.get())){
return mjoin(fmap(stm, f));}
Bartosz Kwaśniewski Functional Programming in C++ 53 / 131
![Page 234: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/234.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Functions)
template<class T>Stream<T> mjoin(Stream<Stream<T>> stm){
while (!stm.isEmpty() && stm.get().isEmpty()){
stm = stm.pop_front();}if (stm.isEmpty()) return Stream<T>();return Stream<T>([stm](){
Stream<T> hd = stm.get();return Cell<T>( hd.get()
, concat(hd.pop_front(), mjoin(stm.pop_front())));});
}
Bartosz Kwaśniewski Functional Programming in C++ 54 / 131
![Page 235: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/235.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Functions)
template<class T, class F>void forEach(Stream<T> strm, F f){
while (!strm.isEmpty()){
f(strm.get());strm = strm.pop_front();
}}
Bartosz Kwaśniewski Functional Programming in C++ 55 / 131
![Page 236: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/236.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Functions)
Stream take(int n) const {if (n == 0 || isEmpty())
return Stream();auto cell = _lazyCell;return Stream([cell, n](){
auto v = cell->get().val();auto t = cell->get().pop_front();return Cell<T>(v, t.take(n - 1));
});}
Bartosz Kwaśniewski Functional Programming in C++ 56 / 131
![Page 237: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/237.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Functions)
Stream<int> ints(int n, int m){
if (n > m)return Stream<int>();
return Stream<int>([n, m](){
return Cell<int>(n, ints(n + 1, m));});
}
Stream<int> intsFrom(int n){
return Stream<int>([n](){
return Cell<int>(n, intsFrom(n + 1));});
}
Bartosz Kwaśniewski Functional Programming in C++ 57 / 131
![Page 238: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/238.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Stream)template<class T>class Stream{
std::shared_ptr <Susp<Cell<T>>> _lazyCell;public:
Stream() {}Stream(std::function<Cell<T>()> f)
: _lazyCell(std::make_shared<Susp<Cell<T>>>(f)){}Stream(Stream && stm)
: _lazyCell(std::move(stm._lazyCell)){}Stream & operator=(Stream && stm){
_lazyCell = std::move(stm._lazyCell);return *this;
}bool isEmpty() const { return !_lazyCell; }T get() const {return _lazyCell->get().val(); }Stream<T> pop_front() const { return _lazyCell->get().pop_front(); }
};
Bartosz Kwaśniewski Functional Programming in C++ 58 / 131
![Page 239: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/239.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Cell)
template<class T>class Cell{public:
Cell() {} // need default constructor for memoizationCell(T v, Stream<T> const & tail)
: _v(v), _tail(tail){}explicit Cell(T v) : _v(v) {}T val() const {
return _v;}Stream<T> pop_front() const {
return _tail;}
private:T _v;Stream<T> _tail;
};
Bartosz Kwaśniewski Functional Programming in C++ 59 / 131
![Page 240: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/240.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Lazy Evaluation: More C++ (Susp)template<class T> class Susp{
static T const & thunkForce(Susp * susp) {return susp->setMemo();
}static T const & thunkGet(Susp * susp) {return susp->getMemo();}T const & getMemo() {return _memo;}T const & setMemo() {
_memo = _f();_thunk = &thunkGet;return getMemo();
}public:
explicit Susp(std::function<T()> f): _f(f), _thunk(&thunkForce), _memo(T()) {}
T const & get() {return _thunk(this); }private:
T const & (*_thunk)(Susp *);mutable T _memo;std::function<T()> _f;
};
Bartosz Kwaśniewski Functional Programming in C++ 60 / 131
![Page 241: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/241.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Lazy Evaluation Summary
▶ No standard support for Laziness in C++
▶ Hundred of lines vs 2 lines▶ Fortunately there are Range V3 library, but about itlater...
Bartosz Kwaśniewski Functional Programming in C++ 61 / 131
![Page 242: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/242.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Lazy Evaluation Summary
▶ No standard support for Laziness in C++▶ Hundred of lines vs 2 lines
▶ Fortunately there are Range V3 library, but about itlater...
Bartosz Kwaśniewski Functional Programming in C++ 61 / 131
![Page 243: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/243.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
C++ Lazy Evaluation Summary
▶ No standard support for Laziness in C++▶ Hundred of lines vs 2 lines▶ Fortunately there are Range V3 library, but about itlater...
Bartosz Kwaśniewski Functional Programming in C++ 61 / 131
![Page 244: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/244.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable Data: Haskell
▶ What is an immutable object?
▶ It is an object whose state cannot be altered after itscreation
▶ What are the features of immutable objects?
▶ Simple and efficient threading, correctness and speed▶ Object creation on modification
Bartosz Kwaśniewski Functional Programming in C++ 62 / 131
![Page 245: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/245.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable Data: Haskell
▶ What is an immutable object?▶ It is an object whose state cannot be altered after its
creation
▶ What are the features of immutable objects?
▶ Simple and efficient threading, correctness and speed▶ Object creation on modification
Bartosz Kwaśniewski Functional Programming in C++ 62 / 131
![Page 246: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/246.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable Data: Haskell
▶ What is an immutable object?▶ It is an object whose state cannot be altered after its
creation▶ What are the features of immutable objects?
▶ Simple and efficient threading, correctness and speed▶ Object creation on modification
Bartosz Kwaśniewski Functional Programming in C++ 62 / 131
![Page 247: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/247.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable Data: Haskell
▶ What is an immutable object?▶ It is an object whose state cannot be altered after its
creation▶ What are the features of immutable objects?
▶ Simple and efficient threading, correctness and speed
▶ Object creation on modification
Bartosz Kwaśniewski Functional Programming in C++ 62 / 131
![Page 248: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/248.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable Data: Haskell
▶ What is an immutable object?▶ It is an object whose state cannot be altered after its
creation▶ What are the features of immutable objects?
▶ Simple and efficient threading, correctness and speed▶ Object creation on modification
Bartosz Kwaśniewski Functional Programming in C++ 62 / 131
![Page 249: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/249.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?
▶ Copy new on modification▶ Based on shallow copy, use of shared pointers▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 250: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/250.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.
▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?
▶ Copy new on modification▶ Based on shallow copy, use of shared pointers▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 251: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/251.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?
▶ Copy new on modification▶ Based on shallow copy, use of shared pointers▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 252: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/252.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?
▶ Copy new on modification▶ Based on shallow copy, use of shared pointers▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 253: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/253.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?
▶ Copy new on modification▶ Based on shallow copy, use of shared pointers▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 254: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/254.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?
▶ Copy new on modification▶ Based on shallow copy, use of shared pointers▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 255: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/255.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?
▶ Copy new on modification▶ Based on shallow copy, use of shared pointers▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 256: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/256.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?▶ Copy new on modification
▶ Based on shallow copy, use of shared pointers▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 257: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/257.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?▶ Copy new on modification▶ Based on shallow copy, use of shared pointers
▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 258: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/258.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++▶ Does C++ support persistent data structures?
▶ No, but you could write one.▶ What are the features of persistent structures?
▶ Immutability: avoiding implicit long-distance couplings(easier maintenance)
▶ Multithreading: thread-safe, read only data structures,no data race
▶ Performance: drawback, naive requires a lot of copying(deep), storing only differences and shallow copying
▶ What are the requirements and techniques?▶ Copy new on modification▶ Based on shallow copy, use of shared pointers▶ Enforce constness
Bartosz Kwaśniewski Functional Programming in C++ 63 / 131
![Page 259: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/259.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++ Example
▶ Persistent data structures should be the part of STLor at least of Boost.
▶ Example: Single Linked List
▶ A list of T is either empty or consists of an element oftype T followed by a list of T.
Bartosz Kwaśniewski Functional Programming in C++ 64 / 131
![Page 260: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/260.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++ Example
▶ Persistent data structures should be the part of STLor at least of Boost.
▶ Example: Single Linked List
▶ A list of T is either empty or consists of an element oftype T followed by a list of T.
Bartosz Kwaśniewski Functional Programming in C++ 64 / 131
![Page 261: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/261.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++ Example
▶ Persistent data structures should be the part of STLor at least of Boost.
▶ Example: Single Linked List▶ A list of T is either empty or consists of an element of
type T followed by a list of T.
Bartosz Kwaśniewski Functional Programming in C++ 64 / 131
![Page 262: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/262.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Immutable and Persistent Data: C++
template<class T> class List {struct Item {
Item(T v, std::shared_ptr<const Item> const & tail): _val(v), _next(tail) {}
T _val;std::shared_ptr<const Item> _next;
}public:
List() : _head(nullptr) {}List(T v, List const & tail) :
_head(std::make_shared<Item>(v, tail._head)) {}bool isEmpty() const { return !_head; }//and many many other functions: pop, push, front map, filter, size,//fold, concat, reverse, foreach,
private:Item const * _head;
};
Bartosz Kwaśniewski Functional Programming in C++ 65 / 131
![Page 263: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/263.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: Haskell▶ What is an Algebraic Data Type?
▶ Composed structure by combination of other types withalgebraic operations
▶ sum: is alternation (A | B, meaning A or B but not both)▶ product: is combination (A B, meaning A and B together)
▶ What are the types of Algebraic Data Type?
▶ parametric: support full parametric polymorphism▶ recursive
data Bool = False | True deriving (Ord)data Day = Monday | ... | Sunday deriving (Eq, Ord, Show, Bounded, Enum)data Point = Point Float Floatdata Shape = Circle Point Float | Rectangle Point Pointdata Maybe a = Nothing | Just a --Parametricdata List a = Nil | Cons a (List a) --Recursivedata Vector a = Vector a a a deriving (Show)data Car a b = Car {model::a, year::b} deriving (Show)
Bartosz Kwaśniewski Functional Programming in C++ 66 / 131
![Page 264: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/264.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: Haskell▶ What is an Algebraic Data Type?
▶ Composed structure by combination of other types withalgebraic operations
▶ sum: is alternation (A | B, meaning A or B but not both)▶ product: is combination (A B, meaning A and B together)
▶ What are the types of Algebraic Data Type?
▶ parametric: support full parametric polymorphism▶ recursive
data Bool = False | True deriving (Ord)data Day = Monday | ... | Sunday deriving (Eq, Ord, Show, Bounded, Enum)data Point = Point Float Floatdata Shape = Circle Point Float | Rectangle Point Pointdata Maybe a = Nothing | Just a --Parametricdata List a = Nil | Cons a (List a) --Recursivedata Vector a = Vector a a a deriving (Show)data Car a b = Car {model::a, year::b} deriving (Show)
Bartosz Kwaśniewski Functional Programming in C++ 66 / 131
![Page 265: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/265.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: Haskell▶ What is an Algebraic Data Type?
▶ Composed structure by combination of other types withalgebraic operations
▶ sum: is alternation (A | B, meaning A or B but not both)
▶ product: is combination (A B, meaning A and B together)▶ What are the types of Algebraic Data Type?
▶ parametric: support full parametric polymorphism▶ recursive
data Bool = False | True deriving (Ord)data Day = Monday | ... | Sunday deriving (Eq, Ord, Show, Bounded, Enum)data Point = Point Float Floatdata Shape = Circle Point Float | Rectangle Point Pointdata Maybe a = Nothing | Just a --Parametricdata List a = Nil | Cons a (List a) --Recursivedata Vector a = Vector a a a deriving (Show)data Car a b = Car {model::a, year::b} deriving (Show)
Bartosz Kwaśniewski Functional Programming in C++ 66 / 131
![Page 266: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/266.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: Haskell▶ What is an Algebraic Data Type?
▶ Composed structure by combination of other types withalgebraic operations
▶ sum: is alternation (A | B, meaning A or B but not both)▶ product: is combination (A B, meaning A and B together)
▶ What are the types of Algebraic Data Type?
▶ parametric: support full parametric polymorphism▶ recursive
data Bool = False | True deriving (Ord)data Day = Monday | ... | Sunday deriving (Eq, Ord, Show, Bounded, Enum)data Point = Point Float Floatdata Shape = Circle Point Float | Rectangle Point Pointdata Maybe a = Nothing | Just a --Parametricdata List a = Nil | Cons a (List a) --Recursivedata Vector a = Vector a a a deriving (Show)data Car a b = Car {model::a, year::b} deriving (Show)
Bartosz Kwaśniewski Functional Programming in C++ 66 / 131
![Page 267: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/267.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: Haskell▶ What is an Algebraic Data Type?
▶ Composed structure by combination of other types withalgebraic operations
▶ sum: is alternation (A | B, meaning A or B but not both)▶ product: is combination (A B, meaning A and B together)
▶ What are the types of Algebraic Data Type?
▶ parametric: support full parametric polymorphism▶ recursive
data Bool = False | True deriving (Ord)data Day = Monday | ... | Sunday deriving (Eq, Ord, Show, Bounded, Enum)data Point = Point Float Floatdata Shape = Circle Point Float | Rectangle Point Pointdata Maybe a = Nothing | Just a --Parametricdata List a = Nil | Cons a (List a) --Recursivedata Vector a = Vector a a a deriving (Show)data Car a b = Car {model::a, year::b} deriving (Show)
Bartosz Kwaśniewski Functional Programming in C++ 66 / 131
![Page 268: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/268.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: Haskell▶ What is an Algebraic Data Type?
▶ Composed structure by combination of other types withalgebraic operations
▶ sum: is alternation (A | B, meaning A or B but not both)▶ product: is combination (A B, meaning A and B together)
▶ What are the types of Algebraic Data Type?▶ parametric: support full parametric polymorphism
▶ recursive
data Bool = False | True deriving (Ord)data Day = Monday | ... | Sunday deriving (Eq, Ord, Show, Bounded, Enum)data Point = Point Float Floatdata Shape = Circle Point Float | Rectangle Point Pointdata Maybe a = Nothing | Just a --Parametricdata List a = Nil | Cons a (List a) --Recursivedata Vector a = Vector a a a deriving (Show)data Car a b = Car {model::a, year::b} deriving (Show)
Bartosz Kwaśniewski Functional Programming in C++ 66 / 131
![Page 269: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/269.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: Haskell▶ What is an Algebraic Data Type?
▶ Composed structure by combination of other types withalgebraic operations
▶ sum: is alternation (A | B, meaning A or B but not both)▶ product: is combination (A B, meaning A and B together)
▶ What are the types of Algebraic Data Type?▶ parametric: support full parametric polymorphism▶ recursive
data Bool = False | True deriving (Ord)data Day = Monday | ... | Sunday deriving (Eq, Ord, Show, Bounded, Enum)data Point = Point Float Floatdata Shape = Circle Point Float | Rectangle Point Pointdata Maybe a = Nothing | Just a --Parametricdata List a = Nil | Cons a (List a) --Recursivedata Vector a = Vector a a a deriving (Show)data Car a b = Car {model::a, year::b} deriving (Show)
Bartosz Kwaśniewski Functional Programming in C++ 66 / 131
![Page 270: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/270.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: Haskell▶ What is an Algebraic Data Type?
▶ Composed structure by combination of other types withalgebraic operations
▶ sum: is alternation (A | B, meaning A or B but not both)▶ product: is combination (A B, meaning A and B together)
▶ What are the types of Algebraic Data Type?▶ parametric: support full parametric polymorphism▶ recursive
data Bool = False | True deriving (Ord)data Day = Monday | ... | Sunday deriving (Eq, Ord, Show, Bounded, Enum)data Point = Point Float Floatdata Shape = Circle Point Float | Rectangle Point Pointdata Maybe a = Nothing | Just a --Parametricdata List a = Nil | Cons a (List a) --Recursivedata Vector a = Vector a a a deriving (Show)data Car a b = Car {model::a, year::b} deriving (Show)
Bartosz Kwaśniewski Functional Programming in C++ 66 / 131
![Page 271: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/271.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: C++
▶ Does C++ support Algebraic Data Types?
▶ No, but there are ...▶ classes (RTTI, virtuality), unions, and std::variant, so
you can have its equivalents
struct nothing {};template<class T>using maybe<T> = boost::variant<T, nothing>;
Bartosz Kwaśniewski Functional Programming in C++ 67 / 131
![Page 272: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/272.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: C++
▶ Does C++ support Algebraic Data Types?▶ No, but there are ...
▶ classes (RTTI, virtuality), unions, and std::variant, soyou can have its equivalents
struct nothing {};template<class T>using maybe<T> = boost::variant<T, nothing>;
Bartosz Kwaśniewski Functional Programming in C++ 67 / 131
![Page 273: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/273.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: C++
▶ Does C++ support Algebraic Data Types?▶ No, but there are ...▶ classes (RTTI, virtuality), unions, and std::variant, so
you can have its equivalents
struct nothing {};template<class T>using maybe<T> = boost::variant<T, nothing>;
Bartosz Kwaśniewski Functional Programming in C++ 67 / 131
![Page 274: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/274.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Algebraic Data Types: C++
▶ Does C++ support Algebraic Data Types?▶ No, but there are ...▶ classes (RTTI, virtuality), unions, and std::variant, so
you can have its equivalents
struct nothing {};template<class T>using maybe<T> = boost::variant<T, nothing>;
Bartosz Kwaśniewski Functional Programming in C++ 67 / 131
![Page 275: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/275.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pattern Matching: Haskell
▶ What is a pattern matching?
▶ Creating a function definition based on pattern▶ Is the elimination construct for algebraic data types
lucky :: Int -> Stringlucky 7 = "LUCKY NUMBER SEVEN!"lucky x = "Sorry, you're out of luck, pal!"
addVectors :: Double a => (a, a) -> (a, a) -> (a, a)addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
Bartosz Kwaśniewski Functional Programming in C++ 68 / 131
![Page 276: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/276.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pattern Matching: Haskell
▶ What is a pattern matching?▶ Creating a function definition based on pattern
▶ Is the elimination construct for algebraic data types
lucky :: Int -> Stringlucky 7 = "LUCKY NUMBER SEVEN!"lucky x = "Sorry, you're out of luck, pal!"
addVectors :: Double a => (a, a) -> (a, a) -> (a, a)addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
Bartosz Kwaśniewski Functional Programming in C++ 68 / 131
![Page 277: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/277.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pattern Matching: Haskell
▶ What is a pattern matching?▶ Creating a function definition based on pattern▶ Is the elimination construct for algebraic data types
lucky :: Int -> Stringlucky 7 = "LUCKY NUMBER SEVEN!"lucky x = "Sorry, you're out of luck, pal!"
addVectors :: Double a => (a, a) -> (a, a) -> (a, a)addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
Bartosz Kwaśniewski Functional Programming in C++ 68 / 131
![Page 278: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/278.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pattern Matching: Haskell
▶ What is a pattern matching?▶ Creating a function definition based on pattern▶ Is the elimination construct for algebraic data types
lucky :: Int -> Stringlucky 7 = "LUCKY NUMBER SEVEN!"lucky x = "Sorry, you're out of luck, pal!"
addVectors :: Double a => (a, a) -> (a, a) -> (a, a)addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
Bartosz Kwaśniewski Functional Programming in C++ 68 / 131
![Page 279: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/279.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pattern Matching: C++
▶ Does C++ support pattern matching?
▶ No, but you have ...▶ Mach7 library (by Yuriy Solodkyy, Gabriel Dos Reis and
Bjarne Stroustrup)
Bartosz Kwaśniewski Functional Programming in C++ 69 / 131
![Page 280: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/280.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pattern Matching: C++
▶ Does C++ support pattern matching?▶ No, but you have ...
▶ Mach7 library (by Yuriy Solodkyy, Gabriel Dos Reis andBjarne Stroustrup)
Bartosz Kwaśniewski Functional Programming in C++ 69 / 131
![Page 281: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/281.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pattern Matching: C++
▶ Does C++ support pattern matching?▶ No, but you have ...▶ Mach7 library (by Yuriy Solodkyy, Gabriel Dos Reis and
Bjarne Stroustrup)
Bartosz Kwaśniewski Functional Programming in C++ 69 / 131
![Page 282: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/282.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Pattern Matching: C++ Mach7 Example
▶ The use of patterns with built-in types
int fib(int n){
var<int> m;
Match(n){
Case(1) return 1;Case(2) return 1;Case(2*m) return sqr(fib(m+1)) - sqr(fib(m-1));Case(2*m+1) return sqr(fib(m+1)) + sqr(fib(m));
}EndMatch
}
Bartosz Kwaśniewski Functional Programming in C++ 70 / 131
![Page 283: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/283.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: Haskell
▶ What is a substitution model?
▶ The most important concept defining functionalparadigm
▶ Process of reduction expressions on equals-for-equalsbasis
▶ Prerequisites?
▶ No assignment operator, equal sign is aequivalence/defining operator
▶ Purity
▶ Example, what does it mean?
x = 5
Bartosz Kwaśniewski Functional Programming in C++ 71 / 131
![Page 284: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/284.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: Haskell
▶ What is a substitution model?▶ The most important concept defining functional
paradigm
▶ Process of reduction expressions on equals-for-equalsbasis
▶ Prerequisites?
▶ No assignment operator, equal sign is aequivalence/defining operator
▶ Purity
▶ Example, what does it mean?
x = 5
Bartosz Kwaśniewski Functional Programming in C++ 71 / 131
![Page 285: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/285.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: Haskell
▶ What is a substitution model?▶ The most important concept defining functional
paradigm▶ Process of reduction expressions on equals-for-equals
basis
▶ Prerequisites?
▶ No assignment operator, equal sign is aequivalence/defining operator
▶ Purity
▶ Example, what does it mean?
x = 5
Bartosz Kwaśniewski Functional Programming in C++ 71 / 131
![Page 286: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/286.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: Haskell
▶ What is a substitution model?▶ The most important concept defining functional
paradigm▶ Process of reduction expressions on equals-for-equals
basis▶ Prerequisites?
▶ No assignment operator, equal sign is aequivalence/defining operator
▶ Purity▶ Example, what does it mean?
x = 5
Bartosz Kwaśniewski Functional Programming in C++ 71 / 131
![Page 287: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/287.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: Haskell
▶ What is a substitution model?▶ The most important concept defining functional
paradigm▶ Process of reduction expressions on equals-for-equals
basis▶ Prerequisites?
▶ No assignment operator, equal sign is aequivalence/defining operator
▶ Purity▶ Example, what does it mean?
x = 5
Bartosz Kwaśniewski Functional Programming in C++ 71 / 131
![Page 288: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/288.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: Haskell
▶ What is a substitution model?▶ The most important concept defining functional
paradigm▶ Process of reduction expressions on equals-for-equals
basis▶ Prerequisites?
▶ No assignment operator, equal sign is aequivalence/defining operator
▶ Purity
▶ Example, what does it mean?
x = 5
Bartosz Kwaśniewski Functional Programming in C++ 71 / 131
![Page 289: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/289.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: Haskell
▶ What is a substitution model?▶ The most important concept defining functional
paradigm▶ Process of reduction expressions on equals-for-equals
basis▶ Prerequisites?
▶ No assignment operator, equal sign is aequivalence/defining operator
▶ Purity▶ Example, what does it mean?
x = 5
Bartosz Kwaśniewski Functional Programming in C++ 71 / 131
![Page 290: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/290.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: Haskell
▶ What is a substitution model?▶ The most important concept defining functional
paradigm▶ Process of reduction expressions on equals-for-equals
basis▶ Prerequisites?
▶ No assignment operator, equal sign is aequivalence/defining operator
▶ Purity▶ Example, what does it mean?
x = 5
Bartosz Kwaśniewski Functional Programming in C++ 71 / 131
![Page 291: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/291.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: C++
▶ Does C++ support substitution model?
▶ No, It can not.▶ Why:
▶ The C++ is not pure (programming with self discipline)▶ Functions are subroutines▶ There is an assignment operator▶ It is imperative language▶ It is impossible to substitute equal-for-equal
Bartosz Kwaśniewski Functional Programming in C++ 72 / 131
![Page 292: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/292.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: C++
▶ Does C++ support substitution model?▶ No, It can not.
▶ Why:
▶ The C++ is not pure (programming with self discipline)▶ Functions are subroutines▶ There is an assignment operator▶ It is imperative language▶ It is impossible to substitute equal-for-equal
Bartosz Kwaśniewski Functional Programming in C++ 72 / 131
![Page 293: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/293.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: C++
▶ Does C++ support substitution model?▶ No, It can not.
▶ Why:
▶ The C++ is not pure (programming with self discipline)▶ Functions are subroutines▶ There is an assignment operator▶ It is imperative language▶ It is impossible to substitute equal-for-equal
Bartosz Kwaśniewski Functional Programming in C++ 72 / 131
![Page 294: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/294.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: C++
▶ Does C++ support substitution model?▶ No, It can not.
▶ Why:▶ The C++ is not pure (programming with self discipline)
▶ Functions are subroutines▶ There is an assignment operator▶ It is imperative language▶ It is impossible to substitute equal-for-equal
Bartosz Kwaśniewski Functional Programming in C++ 72 / 131
![Page 295: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/295.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: C++
▶ Does C++ support substitution model?▶ No, It can not.
▶ Why:▶ The C++ is not pure (programming with self discipline)▶ Functions are subroutines
▶ There is an assignment operator▶ It is imperative language▶ It is impossible to substitute equal-for-equal
Bartosz Kwaśniewski Functional Programming in C++ 72 / 131
![Page 296: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/296.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: C++
▶ Does C++ support substitution model?▶ No, It can not.
▶ Why:▶ The C++ is not pure (programming with self discipline)▶ Functions are subroutines▶ There is an assignment operator
▶ It is imperative language▶ It is impossible to substitute equal-for-equal
Bartosz Kwaśniewski Functional Programming in C++ 72 / 131
![Page 297: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/297.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: C++
▶ Does C++ support substitution model?▶ No, It can not.
▶ Why:▶ The C++ is not pure (programming with self discipline)▶ Functions are subroutines▶ There is an assignment operator▶ It is imperative language
▶ It is impossible to substitute equal-for-equal
Bartosz Kwaśniewski Functional Programming in C++ 72 / 131
![Page 298: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/298.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Substitution Model: C++
▶ Does C++ support substitution model?▶ No, It can not.
▶ Why:▶ The C++ is not pure (programming with self discipline)▶ Functions are subroutines▶ There is an assignment operator▶ It is imperative language▶ It is impossible to substitute equal-for-equal
Bartosz Kwaśniewski Functional Programming in C++ 72 / 131
![Page 299: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/299.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphic Types: Haskell
▶ What are the polymorphic types?
▶ Are types that are universally quantified in some wayover all types
▶ Support parametric polymorphism (generics)▶ Describe families of types
id :: a -> aid a = a
map :: (a->b) -> [a] -> [b]map f [] = []map f (x:xs) = f x : map f xs
Bartosz Kwaśniewski Functional Programming in C++ 73 / 131
![Page 300: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/300.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphic Types: Haskell
▶ What are the polymorphic types?▶ Are types that are universally quantified in some way
over all types
▶ Support parametric polymorphism (generics)▶ Describe families of types
id :: a -> aid a = a
map :: (a->b) -> [a] -> [b]map f [] = []map f (x:xs) = f x : map f xs
Bartosz Kwaśniewski Functional Programming in C++ 73 / 131
![Page 301: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/301.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphic Types: Haskell
▶ What are the polymorphic types?▶ Are types that are universally quantified in some way
over all types▶ Support parametric polymorphism (generics)
▶ Describe families of types
id :: a -> aid a = a
map :: (a->b) -> [a] -> [b]map f [] = []map f (x:xs) = f x : map f xs
Bartosz Kwaśniewski Functional Programming in C++ 73 / 131
![Page 302: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/302.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphic Types: Haskell
▶ What are the polymorphic types?▶ Are types that are universally quantified in some way
over all types▶ Support parametric polymorphism (generics)▶ Describe families of types
id :: a -> aid a = a
map :: (a->b) -> [a] -> [b]map f [] = []map f (x:xs) = f x : map f xs
Bartosz Kwaśniewski Functional Programming in C++ 73 / 131
![Page 303: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/303.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphic Types: Haskell
▶ What are the polymorphic types?▶ Are types that are universally quantified in some way
over all types▶ Support parametric polymorphism (generics)▶ Describe families of types
id :: a -> aid a = a
map :: (a->b) -> [a] -> [b]map f [] = []map f (x:xs) = f x : map f xs
Bartosz Kwaśniewski Functional Programming in C++ 73 / 131
![Page 304: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/304.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Classes and Overloading: Haskell
▶ What are type classes and overloading?
▶ It is an interface that defines some behaviour▶ It is a type system construct that supports ad-hoc
polymorphism (overloading)
class Eq a where(==) :: a -> a -> Bool(/=) :: a -> a -> Boolx == y = not (x /= y)x /= y = not (x == y)
data Foo = Foo {x :: Integer, str :: String}
instance Eq Foo where(Foo x1 str1) == (Foo x2 str2) = (x1 == x2) && (str1 == str2)
Bartosz Kwaśniewski Functional Programming in C++ 74 / 131
![Page 305: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/305.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Classes and Overloading: Haskell
▶ What are type classes and overloading?▶ It is an interface that defines some behaviour
▶ It is a type system construct that supports ad-hocpolymorphism (overloading)
class Eq a where(==) :: a -> a -> Bool(/=) :: a -> a -> Boolx == y = not (x /= y)x /= y = not (x == y)
data Foo = Foo {x :: Integer, str :: String}
instance Eq Foo where(Foo x1 str1) == (Foo x2 str2) = (x1 == x2) && (str1 == str2)
Bartosz Kwaśniewski Functional Programming in C++ 74 / 131
![Page 306: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/306.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Classes and Overloading: Haskell
▶ What are type classes and overloading?▶ It is an interface that defines some behaviour▶ It is a type system construct that supports ad-hoc
polymorphism (overloading)
class Eq a where(==) :: a -> a -> Bool(/=) :: a -> a -> Boolx == y = not (x /= y)x /= y = not (x == y)
data Foo = Foo {x :: Integer, str :: String}
instance Eq Foo where(Foo x1 str1) == (Foo x2 str2) = (x1 == x2) && (str1 == str2)
Bartosz Kwaśniewski Functional Programming in C++ 74 / 131
![Page 307: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/307.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Type Classes and Overloading: Haskell
▶ What are type classes and overloading?▶ It is an interface that defines some behaviour▶ It is a type system construct that supports ad-hoc
polymorphism (overloading)
class Eq a where(==) :: a -> a -> Bool(/=) :: a -> a -> Boolx == y = not (x /= y)x /= y = not (x == y)
data Foo = Foo {x :: Integer, str :: String}
instance Eq Foo where(Foo x1 str1) == (Foo x2 str2) = (x1 == x2) && (str1 == str2)
Bartosz Kwaśniewski Functional Programming in C++ 74 / 131
![Page 308: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/308.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphism: C++
▶ Does C++ support polymorphism?
▶ No▶ Just Kidding... How many polymorphism are inC++?
▶ There are four kind of polymorphism in C++
▶ Subtype polymorphism (runtime)▶ Parametric polymorphism (compile-time)▶ Ad-hoc polymorphism (overloading)▶ Coercion polymorphism (implicit or explicit casting)
Bartosz Kwaśniewski Functional Programming in C++ 75 / 131
![Page 309: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/309.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphism: C++
▶ Does C++ support polymorphism?▶ No
▶ Just Kidding... How many polymorphism are inC++?
▶ There are four kind of polymorphism in C++
▶ Subtype polymorphism (runtime)▶ Parametric polymorphism (compile-time)▶ Ad-hoc polymorphism (overloading)▶ Coercion polymorphism (implicit or explicit casting)
Bartosz Kwaśniewski Functional Programming in C++ 75 / 131
![Page 310: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/310.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphism: C++
▶ Does C++ support polymorphism?▶ No
▶ Just Kidding... How many polymorphism are inC++?
▶ There are four kind of polymorphism in C++
▶ Subtype polymorphism (runtime)▶ Parametric polymorphism (compile-time)▶ Ad-hoc polymorphism (overloading)▶ Coercion polymorphism (implicit or explicit casting)
Bartosz Kwaśniewski Functional Programming in C++ 75 / 131
![Page 311: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/311.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphism: C++
▶ Does C++ support polymorphism?▶ No
▶ Just Kidding... How many polymorphism are inC++?
▶ There are four kind of polymorphism in C++
▶ Subtype polymorphism (runtime)▶ Parametric polymorphism (compile-time)▶ Ad-hoc polymorphism (overloading)▶ Coercion polymorphism (implicit or explicit casting)
Bartosz Kwaśniewski Functional Programming in C++ 75 / 131
![Page 312: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/312.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphism: C++
▶ Does C++ support polymorphism?▶ No
▶ Just Kidding... How many polymorphism are inC++?
▶ There are four kind of polymorphism in C++▶ Subtype polymorphism (runtime)
▶ Parametric polymorphism (compile-time)▶ Ad-hoc polymorphism (overloading)▶ Coercion polymorphism (implicit or explicit casting)
Bartosz Kwaśniewski Functional Programming in C++ 75 / 131
![Page 313: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/313.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphism: C++
▶ Does C++ support polymorphism?▶ No
▶ Just Kidding... How many polymorphism are inC++?
▶ There are four kind of polymorphism in C++▶ Subtype polymorphism (runtime)▶ Parametric polymorphism (compile-time)
▶ Ad-hoc polymorphism (overloading)▶ Coercion polymorphism (implicit or explicit casting)
Bartosz Kwaśniewski Functional Programming in C++ 75 / 131
![Page 314: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/314.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphism: C++
▶ Does C++ support polymorphism?▶ No
▶ Just Kidding... How many polymorphism are inC++?
▶ There are four kind of polymorphism in C++▶ Subtype polymorphism (runtime)▶ Parametric polymorphism (compile-time)▶ Ad-hoc polymorphism (overloading)
▶ Coercion polymorphism (implicit or explicit casting)
Bartosz Kwaśniewski Functional Programming in C++ 75 / 131
![Page 315: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/315.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Polymorphism: C++
▶ Does C++ support polymorphism?▶ No
▶ Just Kidding... How many polymorphism are inC++?
▶ There are four kind of polymorphism in C++▶ Subtype polymorphism (runtime)▶ Parametric polymorphism (compile-time)▶ Ad-hoc polymorphism (overloading)▶ Coercion polymorphism (implicit or explicit casting)
Bartosz Kwaśniewski Functional Programming in C++ 75 / 131
![Page 316: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/316.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: Haskell
▶ What is a list comprehension?
▶ Returns a list of elements created by evaluation of thegenerators
▶ Similar to the mathematical concept of setcomprehensions
print ([x*2 | x <- [1..10], x*2 >= 12])-- prints [12,14,16,18,20]
Bartosz Kwaśniewski Functional Programming in C++ 76 / 131
![Page 317: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/317.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: Haskell
▶ What is a list comprehension?▶ Returns a list of elements created by evaluation of the
generators
▶ Similar to the mathematical concept of setcomprehensions
print ([x*2 | x <- [1..10], x*2 >= 12])-- prints [12,14,16,18,20]
Bartosz Kwaśniewski Functional Programming in C++ 76 / 131
![Page 318: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/318.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: Haskell
▶ What is a list comprehension?▶ Returns a list of elements created by evaluation of the
generators▶ Similar to the mathematical concept of set
comprehensions
print ([x*2 | x <- [1..10], x*2 >= 12])-- prints [12,14,16,18,20]
Bartosz Kwaśniewski Functional Programming in C++ 76 / 131
![Page 319: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/319.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: Haskell
▶ What is a list comprehension?▶ Returns a list of elements created by evaluation of the
generators▶ Similar to the mathematical concept of set
comprehensions
print ([x*2 | x <- [1..10], x*2 >= 12])-- prints [12,14,16,18,20]
Bartosz Kwaśniewski Functional Programming in C++ 76 / 131
![Page 320: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/320.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: C++
▶ Does C++ support list comprehension?
▶ No, not directly, but there is ...▶ Range comprehension, by Range v3 library
▶ Range library for C++11/14/17▶ Basis of a formal proposal to add range support to the
C++ standard library▶ Is lazy
Bartosz Kwaśniewski Functional Programming in C++ 77 / 131
![Page 321: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/321.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: C++
▶ Does C++ support list comprehension?▶ No, not directly, but there is ...
▶ Range comprehension, by Range v3 library
▶ Range library for C++11/14/17▶ Basis of a formal proposal to add range support to the
C++ standard library▶ Is lazy
Bartosz Kwaśniewski Functional Programming in C++ 77 / 131
![Page 322: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/322.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: C++
▶ Does C++ support list comprehension?▶ No, not directly, but there is ...
▶ Range comprehension, by Range v3 library
▶ Range library for C++11/14/17▶ Basis of a formal proposal to add range support to the
C++ standard library▶ Is lazy
Bartosz Kwaśniewski Functional Programming in C++ 77 / 131
![Page 323: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/323.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: C++
▶ Does C++ support list comprehension?▶ No, not directly, but there is ...
▶ Range comprehension, by Range v3 library▶ Range library for C++11/14/17
▶ Basis of a formal proposal to add range support to theC++ standard library
▶ Is lazy
Bartosz Kwaśniewski Functional Programming in C++ 77 / 131
![Page 324: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/324.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: C++
▶ Does C++ support list comprehension?▶ No, not directly, but there is ...
▶ Range comprehension, by Range v3 library▶ Range library for C++11/14/17▶ Basis of a formal proposal to add range support to the
C++ standard library
▶ Is lazy
Bartosz Kwaśniewski Functional Programming in C++ 77 / 131
![Page 325: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/325.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: C++
▶ Does C++ support list comprehension?▶ No, not directly, but there is ...
▶ Range comprehension, by Range v3 library▶ Range library for C++11/14/17▶ Basis of a formal proposal to add range support to the
C++ standard library▶ Is lazy
Bartosz Kwaśniewski Functional Programming in C++ 77 / 131
![Page 326: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/326.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
List Comprehension: C++
// Lazy ranges for generating integer sequencesauto const intsFrom = view::iota;auto const ints = [=](int i, int j) {
return view::take(intsFrom(i), j-i+1);};//Define an infinite range of all the Pythagorean triples:auto triples =view::for_each(intsFrom(1), [](int z) {
return view::for_each(ints(1, z), [=](int x) {return view::for_each(ints(x, z), [=](int y) {
return yield_if(x*x + y*y == z*z, std::make_tuple(x, y, z));});
});});
Bartosz Kwaśniewski Functional Programming in C++ 78 / 131
![Page 327: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/327.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Automatic Memory Management: Haskell
▶ Why purity, laziness and immutability requiresautomatic dynamic memory management?
▶ Immutability forces to produce a lot of temporary data▶ The lifetime of an object can only be determined at
runtime
Bartosz Kwaśniewski Functional Programming in C++ 79 / 131
![Page 328: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/328.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Automatic Memory Management: Haskell
▶ Why purity, laziness and immutability requiresautomatic dynamic memory management?
▶ Immutability forces to produce a lot of temporary data
▶ The lifetime of an object can only be determined atruntime
Bartosz Kwaśniewski Functional Programming in C++ 79 / 131
![Page 329: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/329.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Automatic Memory Management: Haskell
▶ Why purity, laziness and immutability requiresautomatic dynamic memory management?
▶ Immutability forces to produce a lot of temporary data▶ The lifetime of an object can only be determined at
runtime
Bartosz Kwaśniewski Functional Programming in C++ 79 / 131
![Page 330: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/330.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Automatic Memory Management: C++
▶ Does C++ support automatic memorymanagement?
▶ No built-in garbage collector▶ RAII technique realized by smart pointers▶ Semi-automatic memory management
Bartosz Kwaśniewski Functional Programming in C++ 80 / 131
![Page 331: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/331.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Automatic Memory Management: C++
▶ Does C++ support automatic memorymanagement?
▶ No built-in garbage collector
▶ RAII technique realized by smart pointers▶ Semi-automatic memory management
Bartosz Kwaśniewski Functional Programming in C++ 80 / 131
![Page 332: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/332.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Automatic Memory Management: C++
▶ Does C++ support automatic memorymanagement?
▶ No built-in garbage collector▶ RAII technique realized by smart pointers
▶ Semi-automatic memory management
Bartosz Kwaśniewski Functional Programming in C++ 80 / 131
![Page 333: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/333.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Automatic Memory Management: C++
▶ Does C++ support automatic memorymanagement?
▶ No built-in garbage collector▶ RAII technique realized by smart pointers▶ Semi-automatic memory management
Bartosz Kwaśniewski Functional Programming in C++ 80 / 131
![Page 334: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/334.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?
▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 335: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/335.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)
▶ Applicative: a Functor plus lifting values and combiningcomputations (lift, ap)
▶ Monad: an Applicative plus flattening nested levels ofstructure (flatten,chain;bind,return;join,unit)
▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?
▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 336: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/336.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)
▶ Monad: an Applicative plus flattening nested levels ofstructure (flatten,chain;bind,return;join,unit)
▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?
▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 337: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/337.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)
▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?
▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 338: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/338.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?
▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 339: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/339.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)▶ Why are those concepts important?
▶ Formalize many programming structures and techniques
▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?
▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 340: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/340.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important
▶ In C++ getting more and more popular and important▶ C++ Implementations?
▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 341: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/341.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?
▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 342: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/342.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?
▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 343: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/343.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 344: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/344.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those concepts
Bartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 345: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/345.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor, Applicative, Monad▶ What are the category concepts?
▶ Functor: a container that can be mapped over (map)▶ Applicative: a Functor plus lifting values and combining
computations (lift, ap)▶ Monad: an Applicative plus flattening nested levels of
structure (flatten,chain;bind,return;join,unit)▶ Why are those concepts important?
▶ Formalize many programming structures and techniques▶ In Haskell very important▶ In C++ getting more and more popular and important
▶ C++ Implementations?▶ Boost.Hana
▶ Applicative, Comonad, Functor, Group, Monad,MonadPlus, Monoid, Product, Ring
▶ Many C++ constructs turns out those conceptsBartosz Kwaśniewski Functional Programming in C++ 81 / 131
![Page 346: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/346.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in Haskell
▶ What is a functor?
▶ A type (container) that can be mapped (transform) over▶ transforms a container into a new container▶ or lifts a “normal” function into a “higher” function
-- typeclassclass Functor f wherefmap :: (a -> b) -> f a -> f b --fmap :: (a -> b) -> (f a -> f b)
-- list functorinstance Functor [] wherefmap _ [] = []fmap g (x:xs) = g x : fmap g xs -- or we could just say fmap = map
--lawsfmap id = idfmap (g . h) = (fmap g) . (fmap h)
Bartosz Kwaśniewski Functional Programming in C++ 82 / 131
![Page 347: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/347.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in Haskell
▶ What is a functor?▶ A type (container) that can be mapped (transform) over
▶ transforms a container into a new container▶ or lifts a “normal” function into a “higher” function
-- typeclassclass Functor f wherefmap :: (a -> b) -> f a -> f b --fmap :: (a -> b) -> (f a -> f b)
-- list functorinstance Functor [] wherefmap _ [] = []fmap g (x:xs) = g x : fmap g xs -- or we could just say fmap = map
--lawsfmap id = idfmap (g . h) = (fmap g) . (fmap h)
Bartosz Kwaśniewski Functional Programming in C++ 82 / 131
![Page 348: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/348.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in Haskell
▶ What is a functor?▶ A type (container) that can be mapped (transform) over▶ transforms a container into a new container
▶ or lifts a “normal” function into a “higher” function
-- typeclassclass Functor f wherefmap :: (a -> b) -> f a -> f b --fmap :: (a -> b) -> (f a -> f b)
-- list functorinstance Functor [] wherefmap _ [] = []fmap g (x:xs) = g x : fmap g xs -- or we could just say fmap = map
--lawsfmap id = idfmap (g . h) = (fmap g) . (fmap h)
Bartosz Kwaśniewski Functional Programming in C++ 82 / 131
![Page 349: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/349.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in Haskell
▶ What is a functor?▶ A type (container) that can be mapped (transform) over▶ transforms a container into a new container▶ or lifts a “normal” function into a “higher” function
-- typeclassclass Functor f wherefmap :: (a -> b) -> f a -> f b --fmap :: (a -> b) -> (f a -> f b)
-- list functorinstance Functor [] wherefmap _ [] = []fmap g (x:xs) = g x : fmap g xs -- or we could just say fmap = map
--lawsfmap id = idfmap (g . h) = (fmap g) . (fmap h)
Bartosz Kwaśniewski Functional Programming in C++ 82 / 131
![Page 350: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/350.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in Haskell
▶ What is a functor?▶ A type (container) that can be mapped (transform) over▶ transforms a container into a new container▶ or lifts a “normal” function into a “higher” function
-- typeclassclass Functor f wherefmap :: (a -> b) -> f a -> f b --fmap :: (a -> b) -> (f a -> f b)
-- list functorinstance Functor [] wherefmap _ [] = []fmap g (x:xs) = g x : fmap g xs -- or we could just say fmap = map
--lawsfmap id = idfmap (g . h) = (fmap g) . (fmap h)
Bartosz Kwaśniewski Functional Programming in C++ 82 / 131
![Page 351: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/351.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in C++▶ It is a concept that represents types that can bemapped over (by boost.hana))
▶ What are the concrete functors in C++?
▶ STL Containers with std::transform and iterators▶ Boost.Hana: hana::lazy, hana::optional, hana::tuple▶ Range v3: views and ranges
//laws (boost.hana)transform(xs, id) == xstransform(xs, compose(g, f)) == transform(transform(xs, f), g)//examplesauto to_string = [](auto x) { ostringstream ss; ss << x; return ss.str(); };BOOST_HANA_RUNTIME_CHECK(hana::transform(hana::make_tuple(1, '2', "345", string{"67"}), to_string)== hana::make_tuple("1", "2", "345", "67"));
std::vector<int> vi{1,2,3,4,5,6,7,8,9,10};auto rng = vi | view::transform([](int i){return i * 2;});
Bartosz Kwaśniewski Functional Programming in C++ 83 / 131
![Page 352: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/352.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in C++▶ It is a concept that represents types that can bemapped over (by boost.hana))
▶ What are the concrete functors in C++?
▶ STL Containers with std::transform and iterators▶ Boost.Hana: hana::lazy, hana::optional, hana::tuple▶ Range v3: views and ranges
//laws (boost.hana)transform(xs, id) == xstransform(xs, compose(g, f)) == transform(transform(xs, f), g)//examplesauto to_string = [](auto x) { ostringstream ss; ss << x; return ss.str(); };BOOST_HANA_RUNTIME_CHECK(hana::transform(hana::make_tuple(1, '2', "345", string{"67"}), to_string)== hana::make_tuple("1", "2", "345", "67"));
std::vector<int> vi{1,2,3,4,5,6,7,8,9,10};auto rng = vi | view::transform([](int i){return i * 2;});
Bartosz Kwaśniewski Functional Programming in C++ 83 / 131
![Page 353: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/353.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in C++▶ It is a concept that represents types that can bemapped over (by boost.hana))
▶ What are the concrete functors in C++?▶ STL Containers with std::transform and iterators
▶ Boost.Hana: hana::lazy, hana::optional, hana::tuple▶ Range v3: views and ranges
//laws (boost.hana)transform(xs, id) == xstransform(xs, compose(g, f)) == transform(transform(xs, f), g)//examplesauto to_string = [](auto x) { ostringstream ss; ss << x; return ss.str(); };BOOST_HANA_RUNTIME_CHECK(hana::transform(hana::make_tuple(1, '2', "345", string{"67"}), to_string)== hana::make_tuple("1", "2", "345", "67"));
std::vector<int> vi{1,2,3,4,5,6,7,8,9,10};auto rng = vi | view::transform([](int i){return i * 2;});
Bartosz Kwaśniewski Functional Programming in C++ 83 / 131
![Page 354: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/354.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in C++▶ It is a concept that represents types that can bemapped over (by boost.hana))
▶ What are the concrete functors in C++?▶ STL Containers with std::transform and iterators▶ Boost.Hana: hana::lazy, hana::optional, hana::tuple
▶ Range v3: views and ranges//laws (boost.hana)transform(xs, id) == xstransform(xs, compose(g, f)) == transform(transform(xs, f), g)//examplesauto to_string = [](auto x) { ostringstream ss; ss << x; return ss.str(); };BOOST_HANA_RUNTIME_CHECK(hana::transform(hana::make_tuple(1, '2', "345", string{"67"}), to_string)== hana::make_tuple("1", "2", "345", "67"));
std::vector<int> vi{1,2,3,4,5,6,7,8,9,10};auto rng = vi | view::transform([](int i){return i * 2;});
Bartosz Kwaśniewski Functional Programming in C++ 83 / 131
![Page 355: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/355.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in C++▶ It is a concept that represents types that can bemapped over (by boost.hana))
▶ What are the concrete functors in C++?▶ STL Containers with std::transform and iterators▶ Boost.Hana: hana::lazy, hana::optional, hana::tuple▶ Range v3: views and ranges
//laws (boost.hana)transform(xs, id) == xstransform(xs, compose(g, f)) == transform(transform(xs, f), g)//examplesauto to_string = [](auto x) { ostringstream ss; ss << x; return ss.str(); };BOOST_HANA_RUNTIME_CHECK(hana::transform(hana::make_tuple(1, '2', "345", string{"67"}), to_string)== hana::make_tuple("1", "2", "345", "67"));
std::vector<int> vi{1,2,3,4,5,6,7,8,9,10};auto rng = vi | view::transform([](int i){return i * 2;});
Bartosz Kwaśniewski Functional Programming in C++ 83 / 131
![Page 356: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/356.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Functor in C++▶ It is a concept that represents types that can bemapped over (by boost.hana))
▶ What are the concrete functors in C++?▶ STL Containers with std::transform and iterators▶ Boost.Hana: hana::lazy, hana::optional, hana::tuple▶ Range v3: views and ranges
//laws (boost.hana)transform(xs, id) == xstransform(xs, compose(g, f)) == transform(transform(xs, f), g)//examplesauto to_string = [](auto x) { ostringstream ss; ss << x; return ss.str(); };BOOST_HANA_RUNTIME_CHECK(hana::transform(hana::make_tuple(1, '2', "345", string{"67"}), to_string)== hana::make_tuple("1", "2", "345", "67"));
std::vector<int> vi{1,2,3,4,5,6,7,8,9,10};auto rng = vi | view::transform([](int i){return i * 2;});
Bartosz Kwaśniewski Functional Programming in C++ 83 / 131
![Page 357: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/357.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Applicative in Haskell
▶ Apply container of functions to container of values
class Functor f => Applicative f wherepure :: a -> f a(<*>) :: f (a -> b) -> f a -> f b --apply
pure id <*> v = vpure f <*> pure x = pure (f x)u <*> pure y = pure ($ y) <*> uu <*> (v <*> w) = pure (.) <*> u <*> v <*> w
instance Applicative [] wherepure x = [x]fs <*> xs = [f x | f <- fs, x <- xs]
print (pure 123 :: [Int]) --[123]print([(*0),(+100),(^2)] <*> [1,2,3]) --[0,0,0,101,102,103,1,4,9]print([(+),(*)] <*> [1,2] <*> [3,4] )--[(+1),(+2),(*1),(*2)] <*> [3,4], [4,5,5,6,3,4,6,8]
Bartosz Kwaśniewski Functional Programming in C++ 84 / 131
![Page 358: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/358.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Applicative in Haskell
▶ Apply container of functions to container of values
class Functor f => Applicative f wherepure :: a -> f a(<*>) :: f (a -> b) -> f a -> f b --apply
pure id <*> v = vpure f <*> pure x = pure (f x)u <*> pure y = pure ($ y) <*> uu <*> (v <*> w) = pure (.) <*> u <*> v <*> w
instance Applicative [] wherepure x = [x]fs <*> xs = [f x | f <- fs, x <- xs]
print (pure 123 :: [Int]) --[123]print([(*0),(+100),(^2)] <*> [1,2,3]) --[0,0,0,101,102,103,1,4,9]print([(+),(*)] <*> [1,2] <*> [3,4] )--[(+1),(+2),(*1),(*2)] <*> [3,4], [4,5,5,6,3,4,6,8]
Bartosz Kwaśniewski Functional Programming in C++ 84 / 131
![Page 359: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/359.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Applicative in C++
▶ What are applicative in C++?
▶ Represents Functors with the ability to lift values andcombine computations (by boost.hana)
▶ hana::ap, hana::lift
//lawsap(lift<F>(id), xs) == xsap(ap(lift<F>(compose), fs, gs), xs) == ap(fs, ap(gs, xs))ap(lift<F>(f), lift<F>(x)) == lift<F>(f(x))ap(fs, lift<F>(x)) == ap(lift<F>(apply(-, x)), fs)
//examplehana::ap(hana::just([](auto a, auto b, auto c) {return a * b * c;}),hana::just(1), hana::just(2), hana::just(3)
);
Bartosz Kwaśniewski Functional Programming in C++ 85 / 131
![Page 360: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/360.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Applicative in C++
▶ What are applicative in C++?▶ Represents Functors with the ability to lift values and
combine computations (by boost.hana)
▶ hana::ap, hana::lift
//lawsap(lift<F>(id), xs) == xsap(ap(lift<F>(compose), fs, gs), xs) == ap(fs, ap(gs, xs))ap(lift<F>(f), lift<F>(x)) == lift<F>(f(x))ap(fs, lift<F>(x)) == ap(lift<F>(apply(-, x)), fs)
//examplehana::ap(hana::just([](auto a, auto b, auto c) {return a * b * c;}),hana::just(1), hana::just(2), hana::just(3)
);
Bartosz Kwaśniewski Functional Programming in C++ 85 / 131
![Page 361: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/361.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Applicative in C++
▶ What are applicative in C++?▶ Represents Functors with the ability to lift values and
combine computations (by boost.hana)▶ hana::ap, hana::lift
//lawsap(lift<F>(id), xs) == xsap(ap(lift<F>(compose), fs, gs), xs) == ap(fs, ap(gs, xs))ap(lift<F>(f), lift<F>(x)) == lift<F>(f(x))ap(fs, lift<F>(x)) == ap(lift<F>(apply(-, x)), fs)
//examplehana::ap(hana::just([](auto a, auto b, auto c) {return a * b * c;}),hana::just(1), hana::just(2), hana::just(3)
);
Bartosz Kwaśniewski Functional Programming in C++ 85 / 131
![Page 362: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/362.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Applicative in C++
▶ What are applicative in C++?▶ Represents Functors with the ability to lift values and
combine computations (by boost.hana)▶ hana::ap, hana::lift
//lawsap(lift<F>(id), xs) == xsap(ap(lift<F>(compose), fs, gs), xs) == ap(fs, ap(gs, xs))ap(lift<F>(f), lift<F>(x)) == lift<F>(f(x))ap(fs, lift<F>(x)) == ap(lift<F>(apply(-, x)), fs)
//examplehana::ap(hana::just([](auto a, auto b, auto c) {return a * b * c;}),hana::just(1), hana::just(2), hana::just(3)
);
Bartosz Kwaśniewski Functional Programming in C++ 85 / 131
![Page 363: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/363.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?
▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?
▶ Yes, monads are in many places (STL, boost:hana, rangev3)
4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 364: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/364.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory
▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?
▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?
▶ Yes, monads are in many places (STL, boost:hana, rangev3)
4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 365: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/365.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns
▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?
▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?
▶ Yes, monads are in many places (STL, boost:hana, rangev3)
4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 366: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/366.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)
▶ Functor →Applicative →Monad2. Why monads are important in fp?
▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?
▶ Yes, monads are in many places (STL, boost:hana, rangev3)
4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 367: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/367.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?
▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?
▶ Yes, monads are in many places (STL, boost:hana, rangev3)
4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 368: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/368.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?
▶ formalize some useful things (side effects, io)3. Do we have monads in C++?
▶ Yes, monads are in many places (STL, boost:hana, rangev3)
4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 369: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/369.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?
▶ Yes, monads are in many places (STL, boost:hana, rangev3)
4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 370: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/370.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?
▶ Yes, monads are in many places (STL, boost:hana, rangev3)
4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 371: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/371.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?▶ Yes, monads are in many places (STL, boost:hana, range
v3)
4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 372: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/372.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?▶ Yes, monads are in many places (STL, boost:hana, range
v3)4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 373: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/373.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?▶ Yes, monads are in many places (STL, boost:hana, range
v3)4. Are monads important in C++?
▶ no, not really, but ...
▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 374: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/374.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monads in General1. What are monads?
▶ mathematical constructs from category theory▶ programming concepts or design patterns▶ typeclass (or just types)▶ Functor →Applicative →Monad
2. Why monads are important in fp?▶ formalize some useful things (side effects, io)
3. Do we have monads in C++?▶ Yes, monads are in many places (STL, boost:hana, range
v3)4. Are monads important in C++?
▶ no, not really, but ...▶ are getting more and more important
Bartosz Kwaśniewski Functional Programming in C++ 86 / 131
![Page 375: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/375.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Math
▶ A monad over a category C is a triple (T, η, µ) of:
▶ An endofunctor T : C → C▶ A natural unit transformation η : 1C → T▶ A natural join transformation µ : T2 → T
▶ with rules:
▶ An associative square: µ ◦ Tµ = µ ◦ µT▶ a triangle equality: µ ◦ Tη = µ ◦ ηT = 1C
class Endofunctor c t => Monad c t whereeta :: c a (t a)mu :: c (t (t a)) (t a)(>>=) :: (Monad c t) => c a (t b) -> c (t a) (t b)(>>=) f = mu . fmap freturn :: (Monad c t) => c a (t a)return = eta
Bartosz Kwaśniewski Functional Programming in C++ 87 / 131
![Page 376: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/376.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Math
▶ A monad over a category C is a triple (T, η, µ) of:▶ An endofunctor T : C → C
▶ A natural unit transformation η : 1C → T▶ A natural join transformation µ : T2 → T
▶ with rules:
▶ An associative square: µ ◦ Tµ = µ ◦ µT▶ a triangle equality: µ ◦ Tη = µ ◦ ηT = 1C
class Endofunctor c t => Monad c t whereeta :: c a (t a)mu :: c (t (t a)) (t a)(>>=) :: (Monad c t) => c a (t b) -> c (t a) (t b)(>>=) f = mu . fmap freturn :: (Monad c t) => c a (t a)return = eta
Bartosz Kwaśniewski Functional Programming in C++ 87 / 131
![Page 377: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/377.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Math
▶ A monad over a category C is a triple (T, η, µ) of:▶ An endofunctor T : C → C▶ A natural unit transformation η : 1C → T
▶ A natural join transformation µ : T2 → T▶ with rules:
▶ An associative square: µ ◦ Tµ = µ ◦ µT▶ a triangle equality: µ ◦ Tη = µ ◦ ηT = 1C
class Endofunctor c t => Monad c t whereeta :: c a (t a)mu :: c (t (t a)) (t a)(>>=) :: (Monad c t) => c a (t b) -> c (t a) (t b)(>>=) f = mu . fmap freturn :: (Monad c t) => c a (t a)return = eta
Bartosz Kwaśniewski Functional Programming in C++ 87 / 131
![Page 378: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/378.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Math
▶ A monad over a category C is a triple (T, η, µ) of:▶ An endofunctor T : C → C▶ A natural unit transformation η : 1C → T▶ A natural join transformation µ : T2 → T
▶ with rules:
▶ An associative square: µ ◦ Tµ = µ ◦ µT▶ a triangle equality: µ ◦ Tη = µ ◦ ηT = 1C
class Endofunctor c t => Monad c t whereeta :: c a (t a)mu :: c (t (t a)) (t a)(>>=) :: (Monad c t) => c a (t b) -> c (t a) (t b)(>>=) f = mu . fmap freturn :: (Monad c t) => c a (t a)return = eta
Bartosz Kwaśniewski Functional Programming in C++ 87 / 131
![Page 379: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/379.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Math
▶ A monad over a category C is a triple (T, η, µ) of:▶ An endofunctor T : C → C▶ A natural unit transformation η : 1C → T▶ A natural join transformation µ : T2 → T
▶ with rules:
▶ An associative square: µ ◦ Tµ = µ ◦ µT▶ a triangle equality: µ ◦ Tη = µ ◦ ηT = 1C
class Endofunctor c t => Monad c t whereeta :: c a (t a)mu :: c (t (t a)) (t a)(>>=) :: (Monad c t) => c a (t b) -> c (t a) (t b)(>>=) f = mu . fmap freturn :: (Monad c t) => c a (t a)return = eta
Bartosz Kwaśniewski Functional Programming in C++ 87 / 131
![Page 380: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/380.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Math
▶ A monad over a category C is a triple (T, η, µ) of:▶ An endofunctor T : C → C▶ A natural unit transformation η : 1C → T▶ A natural join transformation µ : T2 → T
▶ with rules:▶ An associative square: µ ◦ Tµ = µ ◦ µT
▶ a triangle equality: µ ◦ Tη = µ ◦ ηT = 1C
class Endofunctor c t => Monad c t whereeta :: c a (t a)mu :: c (t (t a)) (t a)(>>=) :: (Monad c t) => c a (t b) -> c (t a) (t b)(>>=) f = mu . fmap freturn :: (Monad c t) => c a (t a)return = eta
Bartosz Kwaśniewski Functional Programming in C++ 87 / 131
![Page 381: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/381.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Math
▶ A monad over a category C is a triple (T, η, µ) of:▶ An endofunctor T : C → C▶ A natural unit transformation η : 1C → T▶ A natural join transformation µ : T2 → T
▶ with rules:▶ An associative square: µ ◦ Tµ = µ ◦ µT▶ a triangle equality: µ ◦ Tη = µ ◦ ηT = 1C
class Endofunctor c t => Monad c t whereeta :: c a (t a)mu :: c (t (t a)) (t a)(>>=) :: (Monad c t) => c a (t b) -> c (t a) (t b)(>>=) f = mu . fmap freturn :: (Monad c t) => c a (t a)return = eta
Bartosz Kwaśniewski Functional Programming in C++ 87 / 131
![Page 382: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/382.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Math
▶ A monad over a category C is a triple (T, η, µ) of:▶ An endofunctor T : C → C▶ A natural unit transformation η : 1C → T▶ A natural join transformation µ : T2 → T
▶ with rules:▶ An associative square: µ ◦ Tµ = µ ◦ µT▶ a triangle equality: µ ◦ Tη = µ ◦ ηT = 1C
class Endofunctor c t => Monad c t whereeta :: c a (t a)mu :: c (t (t a)) (t a)(>>=) :: (Monad c t) => c a (t b) -> c (t a) (t b)(>>=) f = mu . fmap freturn :: (Monad c t) => c a (t a)return = eta
Bartosz Kwaśniewski Functional Programming in C++ 87 / 131
![Page 383: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/383.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Haskell
▶ What are the Monad from programming pov?
▶ bind function: the structure of a monadic computationcan change based on intermediate results
class Applicative m => Monad m wherereturn :: a -> m a --pure(>>=) :: m a -> (a -> m b) -> m b --bind
data Maybe a = Nothing | Just ainstance Monad Maybe where
(Just x) >>= k = k xNothing >>= _ = Nothing
maybeHalf :: Int -> Maybe IntmaybeHalf a
| even a = Just (div a 2)| otherwise = Nothing
Bartosz Kwaśniewski Functional Programming in C++ 88 / 131
![Page 384: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/384.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Haskell
▶ What are the Monad from programming pov?▶ bind function: the structure of a monadic computation
can change based on intermediate results
class Applicative m => Monad m wherereturn :: a -> m a --pure(>>=) :: m a -> (a -> m b) -> m b --bind
data Maybe a = Nothing | Just ainstance Monad Maybe where
(Just x) >>= k = k xNothing >>= _ = Nothing
maybeHalf :: Int -> Maybe IntmaybeHalf a
| even a = Just (div a 2)| otherwise = Nothing
Bartosz Kwaśniewski Functional Programming in C++ 88 / 131
![Page 385: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/385.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in Haskell
▶ What are the Monad from programming pov?▶ bind function: the structure of a monadic computation
can change based on intermediate results
class Applicative m => Monad m wherereturn :: a -> m a --pure(>>=) :: m a -> (a -> m b) -> m b --bind
data Maybe a = Nothing | Just ainstance Monad Maybe where
(Just x) >>= k = k xNothing >>= _ = Nothing
maybeHalf :: Int -> Maybe IntmaybeHalf a
| even a = Just (div a 2)| otherwise = Nothing
Bartosz Kwaśniewski Functional Programming in C++ 88 / 131
![Page 386: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/386.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in C++
▶ What is the Monad from C++ pov?
▶ Represents Applicatives with the ability to flatten nestedlevels of structure (by boost.hana)
hana::flatten(hana::make_tuple(hana::make_tuple(1, 2, 3), hana::make_tuple(4, 5), hana::make_tuple(6, 7, 8, 9)))==hana::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9)
Bartosz Kwaśniewski Functional Programming in C++ 89 / 131
![Page 387: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/387.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in C++
▶ What is the Monad from C++ pov?▶ Represents Applicatives with the ability to flatten nested
levels of structure (by boost.hana)
hana::flatten(hana::make_tuple(hana::make_tuple(1, 2, 3), hana::make_tuple(4, 5), hana::make_tuple(6, 7, 8, 9)))==hana::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9)
Bartosz Kwaśniewski Functional Programming in C++ 89 / 131
![Page 388: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/388.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Category Concepts: Monad in C++
▶ What is the Monad from C++ pov?▶ Represents Applicatives with the ability to flatten nested
levels of structure (by boost.hana)
hana::flatten(hana::make_tuple(hana::make_tuple(1, 2, 3), hana::make_tuple(4, 5), hana::make_tuple(6, 7, 8, 9)))==hana::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9)
Bartosz Kwaśniewski Functional Programming in C++ 89 / 131
![Page 389: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/389.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: Haskell
▶ What are the combinators?
▶ A function which builds program fragments fromprogram fragments: map, filter, fold/reduce
▶ Operating on streams
foldr f z [] = zfoldr f z (x:xs) = f x (foldr f z xs)
foldl f z [] = zfoldl f z (x:xs) = foldl f (f z x) xs
Bartosz Kwaśniewski Functional Programming in C++ 90 / 131
![Page 390: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/390.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: Haskell
▶ What are the combinators?▶ A function which builds program fragments from
program fragments: map, filter, fold/reduce
▶ Operating on streams
foldr f z [] = zfoldr f z (x:xs) = f x (foldr f z xs)
foldl f z [] = zfoldl f z (x:xs) = foldl f (f z x) xs
Bartosz Kwaśniewski Functional Programming in C++ 90 / 131
![Page 391: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/391.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: Haskell
▶ What are the combinators?▶ A function which builds program fragments from
program fragments: map, filter, fold/reduce▶ Operating on streams
foldr f z [] = zfoldr f z (x:xs) = f x (foldr f z xs)
foldl f z [] = zfoldl f z (x:xs) = foldl f (f z x) xs
Bartosz Kwaśniewski Functional Programming in C++ 90 / 131
![Page 392: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/392.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: C++
▶ Does C++ have combinators?
▶ Yes, those are functions from STL and from otherlibraries
▶ STL
▶ transform, reduce, transform_reduce, accumulate, min,max, partition, for_each, for_each_n, all_of, any_of,none_of
▶ Boost.Hana
▶ fold, fold_right, fold_left, min, max, for_each, sum,count, fill, replace, transform, chain, flatten, tap,append, filter, suffix, all, in, find, reverse, scan, zip,zip_with, unfold, partition, insert,
Bartosz Kwaśniewski Functional Programming in C++ 91 / 131
![Page 393: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/393.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: C++
▶ Does C++ have combinators?▶ Yes, those are functions from STL and from other
libraries
▶ STL
▶ transform, reduce, transform_reduce, accumulate, min,max, partition, for_each, for_each_n, all_of, any_of,none_of
▶ Boost.Hana
▶ fold, fold_right, fold_left, min, max, for_each, sum,count, fill, replace, transform, chain, flatten, tap,append, filter, suffix, all, in, find, reverse, scan, zip,zip_with, unfold, partition, insert,
Bartosz Kwaśniewski Functional Programming in C++ 91 / 131
![Page 394: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/394.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: C++
▶ Does C++ have combinators?▶ Yes, those are functions from STL and from other
libraries▶ STL
▶ transform, reduce, transform_reduce, accumulate, min,max, partition, for_each, for_each_n, all_of, any_of,none_of
▶ Boost.Hana
▶ fold, fold_right, fold_left, min, max, for_each, sum,count, fill, replace, transform, chain, flatten, tap,append, filter, suffix, all, in, find, reverse, scan, zip,zip_with, unfold, partition, insert,
Bartosz Kwaśniewski Functional Programming in C++ 91 / 131
![Page 395: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/395.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: C++
▶ Does C++ have combinators?▶ Yes, those are functions from STL and from other
libraries▶ STL
▶ transform, reduce, transform_reduce, accumulate, min,max, partition, for_each, for_each_n, all_of, any_of,none_of
▶ Boost.Hana
▶ fold, fold_right, fold_left, min, max, for_each, sum,count, fill, replace, transform, chain, flatten, tap,append, filter, suffix, all, in, find, reverse, scan, zip,zip_with, unfold, partition, insert,
Bartosz Kwaśniewski Functional Programming in C++ 91 / 131
![Page 396: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/396.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: C++
▶ Does C++ have combinators?▶ Yes, those are functions from STL and from other
libraries▶ STL
▶ transform, reduce, transform_reduce, accumulate, min,max, partition, for_each, for_each_n, all_of, any_of,none_of
▶ Boost.Hana
▶ fold, fold_right, fold_left, min, max, for_each, sum,count, fill, replace, transform, chain, flatten, tap,append, filter, suffix, all, in, find, reverse, scan, zip,zip_with, unfold, partition, insert,
Bartosz Kwaśniewski Functional Programming in C++ 91 / 131
![Page 397: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/397.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: C++
▶ Does C++ have combinators?▶ Yes, those are functions from STL and from other
libraries▶ STL
▶ transform, reduce, transform_reduce, accumulate, min,max, partition, for_each, for_each_n, all_of, any_of,none_of
▶ Boost.Hana▶ fold, fold_right, fold_left, min, max, for_each, sum,
count, fill, replace, transform, chain, flatten, tap,append, filter, suffix, all, in, find, reverse, scan, zip,zip_with, unfold, partition, insert,
Bartosz Kwaśniewski Functional Programming in C++ 91 / 131
![Page 398: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/398.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: C++ Examples
▶ hana::fold combinator
auto to_string = [](auto x) {std::ostringstream ss;ss << x;return ss.str();
};int main() {
auto f = [=](std::string s, auto element) {return "f(" + s + ", " + to_string(element) + ")";
};BOOST_HANA_RUNTIME_CHECK(
hana::fold(hana::make_tuple(2, '3', 4, 5.0), "1", f)==
"f(f(f(f(1, 2), 3), 4), 5)");
}
Bartosz Kwaśniewski Functional Programming in C++ 92 / 131
![Page 399: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/399.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Combinators: C++ Examples
▶ hana::fold combinatorauto to_string = [](auto x) {
std::ostringstream ss;ss << x;return ss.str();
};int main() {
auto f = [=](std::string s, auto element) {return "f(" + s + ", " + to_string(element) + ")";
};BOOST_HANA_RUNTIME_CHECK(
hana::fold(hana::make_tuple(2, '3', 4, 5.0), "1", f)==
"f(f(f(f(1, 2), 3), 4), 5)");
}
Bartosz Kwaśniewski Functional Programming in C++ 92 / 131
![Page 400: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/400.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Good
▶ Good support, by built-in features for:
▶ Type inference: auto and decltype▶ First class functions: lambda expressions and callable
objects▶ Combinators: STL▶ Memory management: RAII including smart pointers▶ Closures: lambda expressions▶ Static typing▶ Polymorphism▶ Algebraic Data Types: std::variant, composition
Bartosz Kwaśniewski Functional Programming in C++ 93 / 131
![Page 401: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/401.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Good
▶ Good support, by built-in features for:▶ Type inference: auto and decltype
▶ First class functions: lambda expressions and callableobjects
▶ Combinators: STL▶ Memory management: RAII including smart pointers▶ Closures: lambda expressions▶ Static typing▶ Polymorphism▶ Algebraic Data Types: std::variant, composition
Bartosz Kwaśniewski Functional Programming in C++ 93 / 131
![Page 402: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/402.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Good
▶ Good support, by built-in features for:▶ Type inference: auto and decltype▶ First class functions: lambda expressions and callable
objects
▶ Combinators: STL▶ Memory management: RAII including smart pointers▶ Closures: lambda expressions▶ Static typing▶ Polymorphism▶ Algebraic Data Types: std::variant, composition
Bartosz Kwaśniewski Functional Programming in C++ 93 / 131
![Page 403: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/403.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Good
▶ Good support, by built-in features for:▶ Type inference: auto and decltype▶ First class functions: lambda expressions and callable
objects▶ Combinators: STL
▶ Memory management: RAII including smart pointers▶ Closures: lambda expressions▶ Static typing▶ Polymorphism▶ Algebraic Data Types: std::variant, composition
Bartosz Kwaśniewski Functional Programming in C++ 93 / 131
![Page 404: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/404.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Good
▶ Good support, by built-in features for:▶ Type inference: auto and decltype▶ First class functions: lambda expressions and callable
objects▶ Combinators: STL▶ Memory management: RAII including smart pointers
▶ Closures: lambda expressions▶ Static typing▶ Polymorphism▶ Algebraic Data Types: std::variant, composition
Bartosz Kwaśniewski Functional Programming in C++ 93 / 131
![Page 405: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/405.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Good
▶ Good support, by built-in features for:▶ Type inference: auto and decltype▶ First class functions: lambda expressions and callable
objects▶ Combinators: STL▶ Memory management: RAII including smart pointers▶ Closures: lambda expressions
▶ Static typing▶ Polymorphism▶ Algebraic Data Types: std::variant, composition
Bartosz Kwaśniewski Functional Programming in C++ 93 / 131
![Page 406: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/406.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Good
▶ Good support, by built-in features for:▶ Type inference: auto and decltype▶ First class functions: lambda expressions and callable
objects▶ Combinators: STL▶ Memory management: RAII including smart pointers▶ Closures: lambda expressions▶ Static typing
▶ Polymorphism▶ Algebraic Data Types: std::variant, composition
Bartosz Kwaśniewski Functional Programming in C++ 93 / 131
![Page 407: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/407.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Good
▶ Good support, by built-in features for:▶ Type inference: auto and decltype▶ First class functions: lambda expressions and callable
objects▶ Combinators: STL▶ Memory management: RAII including smart pointers▶ Closures: lambda expressions▶ Static typing▶ Polymorphism
▶ Algebraic Data Types: std::variant, composition
Bartosz Kwaśniewski Functional Programming in C++ 93 / 131
![Page 408: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/408.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Good
▶ Good support, by built-in features for:▶ Type inference: auto and decltype▶ First class functions: lambda expressions and callable
objects▶ Combinators: STL▶ Memory management: RAII including smart pointers▶ Closures: lambda expressions▶ Static typing▶ Polymorphism▶ Algebraic Data Types: std::variant, composition
Bartosz Kwaśniewski Functional Programming in C++ 93 / 131
![Page 409: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/409.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Some
▶ Some support, by libraries for:
▶ Pattern matching: mach7▶ Range comprehension: Range v3▶ Category concepts: Boost.Hana▶ Laziness: Range v3, Boost.Hana▶ Combinators: Boost.Hana, Range v3▶ Partial application and currying: Boost.Hana
Bartosz Kwaśniewski Functional Programming in C++ 94 / 131
![Page 410: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/410.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Some
▶ Some support, by libraries for:▶ Pattern matching: mach7
▶ Range comprehension: Range v3▶ Category concepts: Boost.Hana▶ Laziness: Range v3, Boost.Hana▶ Combinators: Boost.Hana, Range v3▶ Partial application and currying: Boost.Hana
Bartosz Kwaśniewski Functional Programming in C++ 94 / 131
![Page 411: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/411.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Some
▶ Some support, by libraries for:▶ Pattern matching: mach7▶ Range comprehension: Range v3
▶ Category concepts: Boost.Hana▶ Laziness: Range v3, Boost.Hana▶ Combinators: Boost.Hana, Range v3▶ Partial application and currying: Boost.Hana
Bartosz Kwaśniewski Functional Programming in C++ 94 / 131
![Page 412: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/412.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Some
▶ Some support, by libraries for:▶ Pattern matching: mach7▶ Range comprehension: Range v3▶ Category concepts: Boost.Hana
▶ Laziness: Range v3, Boost.Hana▶ Combinators: Boost.Hana, Range v3▶ Partial application and currying: Boost.Hana
Bartosz Kwaśniewski Functional Programming in C++ 94 / 131
![Page 413: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/413.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Some
▶ Some support, by libraries for:▶ Pattern matching: mach7▶ Range comprehension: Range v3▶ Category concepts: Boost.Hana▶ Laziness: Range v3, Boost.Hana
▶ Combinators: Boost.Hana, Range v3▶ Partial application and currying: Boost.Hana
Bartosz Kwaśniewski Functional Programming in C++ 94 / 131
![Page 414: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/414.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Some
▶ Some support, by libraries for:▶ Pattern matching: mach7▶ Range comprehension: Range v3▶ Category concepts: Boost.Hana▶ Laziness: Range v3, Boost.Hana▶ Combinators: Boost.Hana, Range v3
▶ Partial application and currying: Boost.Hana
Bartosz Kwaśniewski Functional Programming in C++ 94 / 131
![Page 415: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/415.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: Some
▶ Some support, by libraries for:▶ Pattern matching: mach7▶ Range comprehension: Range v3▶ Category concepts: Boost.Hana▶ Laziness: Range v3, Boost.Hana▶ Combinators: Boost.Hana, Range v3▶ Partial application and currying: Boost.Hana
Bartosz Kwaśniewski Functional Programming in C++ 94 / 131
![Page 416: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/416.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: None
▶ None support:
▶ Purity: just self discipline▶ Immutability: custom solutions, self discipline▶ Substitution model: impossible at all
Bartosz Kwaśniewski Functional Programming in C++ 95 / 131
![Page 417: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/417.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: None
▶ None support:▶ Purity: just self discipline
▶ Immutability: custom solutions, self discipline▶ Substitution model: impossible at all
Bartosz Kwaśniewski Functional Programming in C++ 95 / 131
![Page 418: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/418.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: None
▶ None support:▶ Purity: just self discipline▶ Immutability: custom solutions, self discipline
▶ Substitution model: impossible at all
Bartosz Kwaśniewski Functional Programming in C++ 95 / 131
![Page 419: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/419.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary: None
▶ None support:▶ Purity: just self discipline▶ Immutability: custom solutions, self discipline▶ Substitution model: impossible at all
Bartosz Kwaśniewski Functional Programming in C++ 95 / 131
![Page 420: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/420.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Functional Programming
Concepts Support Summary
C++ is in a half way towards full functional paradigmsupport
Bartosz Kwaśniewski Functional Programming in C++ 96 / 131
![Page 421: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/421.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming
Metaprogramming as pure functional language
▶ Does metaprogramming support functionalparadigm?
Bartosz Kwaśniewski Functional Programming in C++ 97 / 131
![Page 422: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/422.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming
Metaprogramming as pure functional language
▶ Does metaprogramming support functionalparadigm?
Bartosz Kwaśniewski Functional Programming in C++ 97 / 131
![Page 423: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/423.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming
“Within C++, there is a much smaller and cleaner languagestruggling to get out”
Bjarne Stroustrup
▶ C++ metaprogramming is a pure functionalcompile-time programming
▶ Haskell could be used as a pseudocode for it
Bartosz Kwaśniewski Functional Programming in C++ 98 / 131
![Page 424: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/424.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming
“Within C++, there is a much smaller and cleaner languagestruggling to get out”
Bjarne Stroustrup
▶ C++ metaprogramming is a pure functionalcompile-time programming
▶ Haskell could be used as a pseudocode for it
Bartosz Kwaśniewski Functional Programming in C++ 98 / 131
![Page 425: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/425.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming
“Within C++, there is a much smaller and cleaner languagestruggling to get out”
Bjarne Stroustrup
▶ C++ metaprogramming is a pure functionalcompile-time programming
▶ Haskell could be used as a pseudocode for it
Bartosz Kwaśniewski Functional Programming in C++ 98 / 131
![Page 426: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/426.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Characteristics
▶ Why MP is pure functional language
▶ no side effects by design
▶ no mutable variables▶ no assignment operator*▶ constant expressions▶ no loops, only recursions*
Bartosz Kwaśniewski Functional Programming in C++ 99 / 131
![Page 427: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/427.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Characteristics
▶ Why MP is pure functional language▶ no side effects by design
▶ no mutable variables▶ no assignment operator*▶ constant expressions▶ no loops, only recursions*
Bartosz Kwaśniewski Functional Programming in C++ 99 / 131
![Page 428: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/428.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Characteristics
▶ Why MP is pure functional language▶ no side effects by design
▶ no mutable variables
▶ no assignment operator*▶ constant expressions▶ no loops, only recursions*
Bartosz Kwaśniewski Functional Programming in C++ 99 / 131
![Page 429: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/429.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Characteristics
▶ Why MP is pure functional language▶ no side effects by design
▶ no mutable variables▶ no assignment operator*
▶ constant expressions▶ no loops, only recursions*
Bartosz Kwaśniewski Functional Programming in C++ 99 / 131
![Page 430: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/430.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Characteristics
▶ Why MP is pure functional language▶ no side effects by design
▶ no mutable variables▶ no assignment operator*▶ constant expressions
▶ no loops, only recursions*
Bartosz Kwaśniewski Functional Programming in C++ 99 / 131
![Page 431: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/431.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Characteristics
▶ Why MP is pure functional language▶ no side effects by design
▶ no mutable variables▶ no assignment operator*▶ constant expressions▶ no loops, only recursions*
Bartosz Kwaśniewski Functional Programming in C++ 99 / 131
![Page 432: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/432.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Haskell Factorial
▶ Factorial in Haskell
fact 0 = 1fact n = n * fact(n - 1)main = print(fact 5) --prints 120
▶ two liner▶ pattern matching with two definitions▶ recursion
Bartosz Kwaśniewski Functional Programming in C++ 100 / 131
![Page 433: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/433.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Haskell Factorial
▶ Factorial in Haskellfact 0 = 1fact n = n * fact(n - 1)main = print(fact 5) --prints 120
▶ two liner▶ pattern matching with two definitions▶ recursion
Bartosz Kwaśniewski Functional Programming in C++ 100 / 131
![Page 434: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/434.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Haskell Factorial
▶ Factorial in Haskellfact 0 = 1fact n = n * fact(n - 1)main = print(fact 5) --prints 120
▶ two liner
▶ pattern matching with two definitions▶ recursion
Bartosz Kwaśniewski Functional Programming in C++ 100 / 131
![Page 435: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/435.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Haskell Factorial
▶ Factorial in Haskellfact 0 = 1fact n = n * fact(n - 1)main = print(fact 5) --prints 120
▶ two liner▶ pattern matching with two definitions
▶ recursion
Bartosz Kwaśniewski Functional Programming in C++ 100 / 131
![Page 436: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/436.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Metaprogramming: Haskell Factorial
▶ Factorial in Haskellfact 0 = 1fact n = n * fact(n - 1)main = print(fact 5) --prints 120
▶ two liner▶ pattern matching with two definitions▶ recursion
Bartosz Kwaśniewski Functional Programming in C++ 100 / 131
![Page 437: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/437.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
TMP Factorial
▶ Factorial in C++98 Template Meta Programming
template <int n> struct fact98 {static const int value = n * fact98<n-1>::value;
};
template <> struct fact98<0> {static const int value = 1;
};
std::cout << fact98<5>::value << std::endl; //prints 120
▶ many liner▶ template instantiation acts as function calls▶ pattern matching implemented as specialization
Bartosz Kwaśniewski Functional Programming in C++ 101 / 131
![Page 438: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/438.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
TMP Factorial
▶ Factorial in C++98 Template Meta Programmingtemplate <int n> struct fact98 {
static const int value = n * fact98<n-1>::value;};
template <> struct fact98<0> {static const int value = 1;
};
std::cout << fact98<5>::value << std::endl; //prints 120
▶ many liner▶ template instantiation acts as function calls▶ pattern matching implemented as specialization
Bartosz Kwaśniewski Functional Programming in C++ 101 / 131
![Page 439: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/439.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
TMP Factorial
▶ Factorial in C++98 Template Meta Programmingtemplate <int n> struct fact98 {
static const int value = n * fact98<n-1>::value;};
template <> struct fact98<0> {static const int value = 1;
};
std::cout << fact98<5>::value << std::endl; //prints 120
▶ many liner
▶ template instantiation acts as function calls▶ pattern matching implemented as specialization
Bartosz Kwaśniewski Functional Programming in C++ 101 / 131
![Page 440: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/440.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
TMP Factorial
▶ Factorial in C++98 Template Meta Programmingtemplate <int n> struct fact98 {
static const int value = n * fact98<n-1>::value;};
template <> struct fact98<0> {static const int value = 1;
};
std::cout << fact98<5>::value << std::endl; //prints 120
▶ many liner▶ template instantiation acts as function calls
▶ pattern matching implemented as specialization
Bartosz Kwaśniewski Functional Programming in C++ 101 / 131
![Page 441: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/441.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
TMP Factorial
▶ Factorial in C++98 Template Meta Programmingtemplate <int n> struct fact98 {
static const int value = n * fact98<n-1>::value;};
template <> struct fact98<0> {static const int value = 1;
};
std::cout << fact98<5>::value << std::endl; //prints 120
▶ many liner▶ template instantiation acts as function calls▶ pattern matching implemented as specialization
Bartosz Kwaśniewski Functional Programming in C++ 101 / 131
![Page 442: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/442.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Constexpr C++11 factorial
▶ Factorial in C++11 Constexpr Meta Programming
constexpr int fact11(int n) {return n <= 1? 1 : (n * fact11(n - 1));
}std::cout << Int<fact11(5)>::value << std::endl; //prints 120
▶ looks better
Bartosz Kwaśniewski Functional Programming in C++ 102 / 131
![Page 443: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/443.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Constexpr C++11 factorial
▶ Factorial in C++11 Constexpr Meta Programmingconstexpr int fact11(int n) {
return n <= 1? 1 : (n * fact11(n - 1));}std::cout << Int<fact11(5)>::value << std::endl; //prints 120
▶ looks better
Bartosz Kwaśniewski Functional Programming in C++ 102 / 131
![Page 444: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/444.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Constexpr C++11 factorial
▶ Factorial in C++11 Constexpr Meta Programmingconstexpr int fact11(int n) {
return n <= 1? 1 : (n * fact11(n - 1));}std::cout << Int<fact11(5)>::value << std::endl; //prints 120
▶ looks better
Bartosz Kwaśniewski Functional Programming in C++ 102 / 131
![Page 445: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/445.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Constexpr C++14 factorial
▶ Factorial in C++14 Constexpr Meta Programming
constexpr int fact14(int n) {switch(n) {
case 0: return 1;default: return n * fact14(n-1);
}}constexpr int fact14loop(int n) {
int s = 1;for (int i=1; i<=n; i++) {s = s * i;}return s;
}
▶ pure functional compile-time meta programming inimperative way
Bartosz Kwaśniewski Functional Programming in C++ 103 / 131
![Page 446: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/446.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Constexpr C++14 factorial
▶ Factorial in C++14 Constexpr Meta Programmingconstexpr int fact14(int n) {
switch(n) {case 0: return 1;default: return n * fact14(n-1);
}}constexpr int fact14loop(int n) {
int s = 1;for (int i=1; i<=n; i++) {s = s * i;}return s;
}
▶ pure functional compile-time meta programming inimperative way
Bartosz Kwaśniewski Functional Programming in C++ 103 / 131
![Page 447: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/447.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Constexpr C++14 factorial
▶ Factorial in C++14 Constexpr Meta Programmingconstexpr int fact14(int n) {
switch(n) {case 0: return 1;default: return n * fact14(n-1);
}}constexpr int fact14loop(int n) {
int s = 1;for (int i=1; i<=n; i++) {s = s * i;}return s;
}
▶ pure functional compile-time meta programming inimperative way
Bartosz Kwaśniewski Functional Programming in C++ 103 / 131
![Page 448: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/448.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
▶ Within imperative language there is pure functionalone with an imperative looking syntax
Bartosz Kwaśniewski Functional Programming in C++ 104 / 131
![Page 449: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/449.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Predicates: Haskell
▶ Predicate in Haskell:
isZero 0 = TrueisZero n = False
▶ Predicate in C+98 TMP:template <int> struct isZero {
static const bool value = false;};template <> struct isZero<0> {
static const bool value = true;};
▶ Almost the same
Bartosz Kwaśniewski Functional Programming in C++ 105 / 131
![Page 450: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/450.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Predicates: Haskell
▶ Predicate in Haskell:isZero 0 = TrueisZero n = False
▶ Predicate in C+98 TMP:
template <int> struct isZero {static const bool value = false;
};template <> struct isZero<0> {
static const bool value = true;};
▶ Almost the same
Bartosz Kwaśniewski Functional Programming in C++ 105 / 131
![Page 451: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/451.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Predicates: Haskell
▶ Predicate in Haskell:isZero 0 = TrueisZero n = False
▶ Predicate in C+98 TMP:
template <int> struct isZero {static const bool value = false;
};template <> struct isZero<0> {
static const bool value = true;};
▶ Almost the same
Bartosz Kwaśniewski Functional Programming in C++ 105 / 131
![Page 452: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/452.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Predicates: Haskell
▶ Predicate in Haskell:isZero 0 = TrueisZero n = False
▶ Predicate in C+98 TMP:template <int> struct isZero {
static const bool value = false;};template <> struct isZero<0> {
static const bool value = true;};
▶ Almost the same
Bartosz Kwaśniewski Functional Programming in C++ 105 / 131
![Page 453: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/453.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Predicates: Haskell
▶ Predicate in Haskell:isZero 0 = TrueisZero n = False
▶ Predicate in C+98 TMP:template <int> struct isZero {
static const bool value = false;};template <> struct isZero<0> {
static const bool value = true;};
▶ Almost the same
Bartosz Kwaśniewski Functional Programming in C++ 105 / 131
![Page 454: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/454.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
OR combinator and Closure: Haskell
▶ Or combinator:
ori :: (Int -> Bool) -> (Int -> Bool) -> Int -> Boolori f g = \x -> (f x) || (g x)main = print(ori isZero isOne 0) --prints True
▶ Higher order function, lambda with closure, predicatecomposition
Bartosz Kwaśniewski Functional Programming in C++ 106 / 131
![Page 455: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/455.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
OR combinator and Closure: Haskell
▶ Or combinator:ori :: (Int -> Bool) -> (Int -> Bool) -> Int -> Boolori f g = \x -> (f x) || (g x)main = print(ori isZero isOne 0) --prints True
▶ Higher order function, lambda with closure, predicatecomposition
Bartosz Kwaśniewski Functional Programming in C++ 106 / 131
![Page 456: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/456.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
OR combinator and Closure: Haskell
▶ Or combinator:ori :: (Int -> Bool) -> (Int -> Bool) -> Int -> Boolori f g = \x -> (f x) || (g x)main = print(ori isZero isOne 0) --prints True
▶ Higher order function, lambda with closure, predicatecomposition
Bartosz Kwaśniewski Functional Programming in C++ 106 / 131
![Page 457: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/457.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
OR combinator and Closure: TMP C++▶ Or with closure in C++98 TMP:
//inttemplate <template <int> class f, template <int> class g>struct or {
template<int n> struct closure {static const bool value = f<n>::value || g<n>::value;};
};
//any typetemplate <template <class> class F, template <class> class G>struct Or {
template<class T> struct closure {static const bool value = F<T>::value || G<T>::value;
};};
cout << or<isZero,isOne>::closure<0>::value; //prints 1cout << Or<IsIntZero, IsIntOne>::closure<Int<0>>::value; //prints 1
Bartosz Kwaśniewski Functional Programming in C++ 107 / 131
![Page 458: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/458.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
OR combinator and Closure: TMP C++▶ Or with closure in C++98 TMP:
//inttemplate <template <int> class f, template <int> class g>struct or {
template<int n> struct closure {static const bool value = f<n>::value || g<n>::value;};
};
//any typetemplate <template <class> class F, template <class> class G>struct Or {
template<class T> struct closure {static const bool value = F<T>::value || G<T>::value;
};};
cout << or<isZero,isOne>::closure<0>::value; //prints 1cout << Or<IsIntZero, IsIntOne>::closure<Int<0>>::value; //prints 1
Bartosz Kwaśniewski Functional Programming in C++ 107 / 131
![Page 459: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/459.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
all: haskell
▶ Higher order function operating on list
▶ All algorithm in haskell
all pred [] = Trueall pred (head:tail) = (pred head) && (all pred tail)
--all application on int list with two predicate and or combinatormain = print(alli (ori isZero isOne) [1,1,0,1,0]) --true
▶ pattern matching, recursion and composition
Bartosz Kwaśniewski Functional Programming in C++ 108 / 131
![Page 460: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/460.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
all: haskell
▶ Higher order function operating on list▶ All algorithm in haskell
all pred [] = Trueall pred (head:tail) = (pred head) && (all pred tail)
--all application on int list with two predicate and or combinatormain = print(alli (ori isZero isOne) [1,1,0,1,0]) --true
▶ pattern matching, recursion and composition
Bartosz Kwaśniewski Functional Programming in C++ 108 / 131
![Page 461: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/461.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
all: haskell
▶ Higher order function operating on list▶ All algorithm in haskell
all pred [] = Trueall pred (head:tail) = (pred head) && (all pred tail)
--all application on int list with two predicate and or combinatormain = print(alli (ori isZero isOne) [1,1,0,1,0]) --true
▶ pattern matching, recursion and composition
Bartosz Kwaśniewski Functional Programming in C++ 108 / 131
![Page 462: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/462.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
all: haskell
▶ Higher order function operating on list▶ All algorithm in haskell
all pred [] = Trueall pred (head:tail) = (pred head) && (all pred tail)
--all application on int list with two predicate and or combinatormain = print(alli (ori isZero isOne) [1,1,0,1,0]) --true
▶ pattern matching, recursion and composition
Bartosz Kwaśniewski Functional Programming in C++ 108 / 131
![Page 463: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/463.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
all: TMP C++
▶ The all algorithm equivalence in C++
template <template <int> class pred, int... list> struct alli;
template <template <int> class pred>struct alli<pred> { static const bool value = true; };
template <template <int> class pred, int head, int... tail>struct alli<pred, head, tail...> {
static const bool value = pred<head>::value && alli<pred, tail...>::value;};
cout << alli<ori<isZero,isOne>::closure,1,1,0,1,0>::value;
▶ Forward declaration, template specialization,variadic templates and parameter pack
Bartosz Kwaśniewski Functional Programming in C++ 109 / 131
![Page 464: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/464.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
all: TMP C++
▶ The all algorithm equivalence in C++
template <template <int> class pred, int... list> struct alli;
template <template <int> class pred>struct alli<pred> { static const bool value = true; };
template <template <int> class pred, int head, int... tail>struct alli<pred, head, tail...> {
static const bool value = pred<head>::value && alli<pred, tail...>::value;};
cout << alli<ori<isZero,isOne>::closure,1,1,0,1,0>::value;
▶ Forward declaration, template specialization,variadic templates and parameter pack
Bartosz Kwaśniewski Functional Programming in C++ 109 / 131
![Page 465: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/465.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
all: TMP C++
▶ The all algorithm equivalence in C++
template <template <int> class pred, int... list> struct alli;
template <template <int> class pred>struct alli<pred> { static const bool value = true; };
template <template <int> class pred, int head, int... tail>struct alli<pred, head, tail...> {
static const bool value = pred<head>::value && alli<pred, tail...>::value;};
cout << alli<ori<isZero,isOne>::closure,1,1,0,1,0>::value;
▶ Forward declaration, template specialization,variadic templates and parameter pack
Bartosz Kwaśniewski Functional Programming in C++ 109 / 131
![Page 466: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/466.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
fold right: haskell
▶ The foldr algorithm in Haskell
--definitionfoldr f init [] = initfoldr f init (head:tail) = f head (foldr f init tail)
--with anonymous functionprint(foldr (\n acc -> n + acc) 1 [1,2,3]) --prints 7
--with auxilary functionadd n acc = n + accprint(foldr add 1 [1,2,3]) --prints 7
▶ lambda expression, anonymous function, recursion,pattern matching
Bartosz Kwaśniewski Functional Programming in C++ 110 / 131
![Page 467: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/467.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
fold right: haskell
▶ The foldr algorithm in Haskell
--definitionfoldr f init [] = initfoldr f init (head:tail) = f head (foldr f init tail)
--with anonymous functionprint(foldr (\n acc -> n + acc) 1 [1,2,3]) --prints 7
--with auxilary functionadd n acc = n + accprint(foldr add 1 [1,2,3]) --prints 7
▶ lambda expression, anonymous function, recursion,pattern matching
Bartosz Kwaśniewski Functional Programming in C++ 110 / 131
![Page 468: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/468.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
fold right: haskell
▶ The foldr algorithm in Haskell
--definitionfoldr f init [] = initfoldr f init (head:tail) = f head (foldr f init tail)
--with anonymous functionprint(foldr (\n acc -> n + acc) 1 [1,2,3]) --prints 7
--with auxilary functionadd n acc = n + accprint(foldr add 1 [1,2,3]) --prints 7
▶ lambda expression, anonymous function, recursion,pattern matching
Bartosz Kwaśniewski Functional Programming in C++ 110 / 131
![Page 469: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/469.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
fold right: TMP C++
▶ The foldr algorithm equivalence in C++
template<template<int, int> class, int, int...> struct foldr;
template<template<int, int> class f, int init> structfoldr<f, init> {static const int value = init; };
template<template<int, int> class f, int init, int head, int... tail>struct foldr<f, init, head, tail...> {
static const int value = f<head, foldr<f, init, tail...>::value>::value;};
template<int a, int b> struct add { static const int value = a + b;};std::cout << foldr<add, 1, 1, 2, 3>::value; //prints 7
▶ Fortunately there is fold C++17 expression
Bartosz Kwaśniewski Functional Programming in C++ 111 / 131
![Page 470: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/470.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
fold right: TMP C++
▶ The foldr algorithm equivalence in C++
template<template<int, int> class, int, int...> struct foldr;
template<template<int, int> class f, int init> structfoldr<f, init> {static const int value = init; };
template<template<int, int> class f, int init, int head, int... tail>struct foldr<f, init, head, tail...> {
static const int value = f<head, foldr<f, init, tail...>::value>::value;};
template<int a, int b> struct add { static const int value = a + b;};std::cout << foldr<add, 1, 1, 2, 3>::value; //prints 7
▶ Fortunately there is fold C++17 expression
Bartosz Kwaśniewski Functional Programming in C++ 111 / 131
![Page 471: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/471.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
fold right: TMP C++
▶ The foldr algorithm equivalence in C++
template<template<int, int> class, int, int...> struct foldr;
template<template<int, int> class f, int init> structfoldr<f, init> {static const int value = init; };
template<template<int, int> class f, int init, int head, int... tail>struct foldr<f, init, head, tail...> {
static const int value = f<head, foldr<f, init, tail...>::value>::value;};
template<int a, int b> struct add { static const int value = a + b;};std::cout << foldr<add, 1, 1, 2, 3>::value; //prints 7
▶ Fortunately there is fold C++17 expression
Bartosz Kwaśniewski Functional Programming in C++ 111 / 131
![Page 472: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/472.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: haskell
▶ Sum algorithm in Haskell
▶ with pattern matching and recursion:
sum1 [] = 0sum1 (head:tail) = head + (sum1 tail)print(sum1 [1,2,3]) --prints 6
▶ with function composition and lambda:sum2 lst = foldr2 (\n acc -> n + acc) 0 lstprint(sum2 [1,2,3]) --prints 6
Bartosz Kwaśniewski Functional Programming in C++ 112 / 131
![Page 473: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/473.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: haskell
▶ Sum algorithm in Haskell▶ with pattern matching and recursion:
sum1 [] = 0sum1 (head:tail) = head + (sum1 tail)print(sum1 [1,2,3]) --prints 6
▶ with function composition and lambda:sum2 lst = foldr2 (\n acc -> n + acc) 0 lstprint(sum2 [1,2,3]) --prints 6
Bartosz Kwaśniewski Functional Programming in C++ 112 / 131
![Page 474: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/474.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: haskell
▶ Sum algorithm in Haskell▶ with pattern matching and recursion:
sum1 [] = 0sum1 (head:tail) = head + (sum1 tail)print(sum1 [1,2,3]) --prints 6
▶ with function composition and lambda:
sum2 lst = foldr2 (\n acc -> n + acc) 0 lstprint(sum2 [1,2,3]) --prints 6
Bartosz Kwaśniewski Functional Programming in C++ 112 / 131
![Page 475: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/475.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: haskell
▶ Sum algorithm in Haskell▶ with pattern matching and recursion:
sum1 [] = 0sum1 (head:tail) = head + (sum1 tail)print(sum1 [1,2,3]) --prints 6
▶ with function composition and lambda:
sum2 lst = foldr2 (\n acc -> n + acc) 0 lstprint(sum2 [1,2,3]) --prints 6
Bartosz Kwaśniewski Functional Programming in C++ 112 / 131
![Page 476: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/476.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: haskell
▶ Sum algorithm in Haskell▶ with pattern matching and recursion:
sum1 [] = 0sum1 (head:tail) = head + (sum1 tail)print(sum1 [1,2,3]) --prints 6
▶ with function composition and lambda:sum2 lst = foldr2 (\n acc -> n + acc) 0 lstprint(sum2 [1,2,3]) --prints 6
Bartosz Kwaśniewski Functional Programming in C++ 112 / 131
![Page 477: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/477.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: TMP C++▶ The sum algorithm equivalence in C++
▶ TMP C++ directly:
template <int...> struct sum;template <> struct sum<> {
static const int value = 0;};template <int head, int... tail> struct sum<head, tail...> {
static const int value = head + sum<tail...>::value;};std::cout << sum<1,2,3>::value << std::endl; //prints 6
▶ TMP C++ with composition:template <int... ints> struct sum2 {
static const int value = foldr<add,0,ints...>::value;};std::cout << sum2<1,2,3>::value << std::endl;//prints 6
Bartosz Kwaśniewski Functional Programming in C++ 113 / 131
![Page 478: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/478.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: TMP C++▶ The sum algorithm equivalence in C++▶ TMP C++ directly:
template <int...> struct sum;template <> struct sum<> {
static const int value = 0;};template <int head, int... tail> struct sum<head, tail...> {
static const int value = head + sum<tail...>::value;};std::cout << sum<1,2,3>::value << std::endl; //prints 6
▶ TMP C++ with composition:template <int... ints> struct sum2 {
static const int value = foldr<add,0,ints...>::value;};std::cout << sum2<1,2,3>::value << std::endl;//prints 6
Bartosz Kwaśniewski Functional Programming in C++ 113 / 131
![Page 479: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/479.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: TMP C++▶ The sum algorithm equivalence in C++▶ TMP C++ directly:
template <int...> struct sum;template <> struct sum<> {
static const int value = 0;};template <int head, int... tail> struct sum<head, tail...> {
static const int value = head + sum<tail...>::value;};std::cout << sum<1,2,3>::value << std::endl; //prints 6
▶ TMP C++ with composition:
template <int... ints> struct sum2 {static const int value = foldr<add,0,ints...>::value;
};std::cout << sum2<1,2,3>::value << std::endl;//prints 6
Bartosz Kwaśniewski Functional Programming in C++ 113 / 131
![Page 480: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/480.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: TMP C++▶ The sum algorithm equivalence in C++▶ TMP C++ directly:
template <int...> struct sum;template <> struct sum<> {
static const int value = 0;};template <int head, int... tail> struct sum<head, tail...> {
static const int value = head + sum<tail...>::value;};std::cout << sum<1,2,3>::value << std::endl; //prints 6
▶ TMP C++ with composition:
template <int... ints> struct sum2 {static const int value = foldr<add,0,ints...>::value;
};std::cout << sum2<1,2,3>::value << std::endl;//prints 6
Bartosz Kwaśniewski Functional Programming in C++ 113 / 131
![Page 481: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/481.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: TMP C++▶ The sum algorithm equivalence in C++▶ TMP C++ directly:
template <int...> struct sum;template <> struct sum<> {
static const int value = 0;};template <int head, int... tail> struct sum<head, tail...> {
static const int value = head + sum<tail...>::value;};std::cout << sum<1,2,3>::value << std::endl; //prints 6
▶ TMP C++ with composition:template <int... ints> struct sum2 {
static const int value = foldr<add,0,ints...>::value;};std::cout << sum2<1,2,3>::value << std::endl;//prints 6
Bartosz Kwaśniewski Functional Programming in C++ 113 / 131
![Page 482: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/482.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: C++17
▶ By C++17 fold expression
template <int... ints> struct sum3 {static const int value = (ints + ...);
};std::cout << sum3<1,2,3>::value << std::endl;//prints 6
Bartosz Kwaśniewski Functional Programming in C++ 114 / 131
![Page 483: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/483.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
sum: C++17
▶ By C++17 fold expressiontemplate <int... ints> struct sum3 {
static const int value = (ints + ...);};std::cout << sum3<1,2,3>::value << std::endl;//prints 6
Bartosz Kwaśniewski Functional Programming in C++ 114 / 131
![Page 484: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/484.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: haskell
▶ Count algorithm in Haskell
▶ with recursion:
count1 [] = 0count1 (head:tail) = 1 + count1 tail
▶ with list comprehension:count2 lst = sum2 [1 | x <- lst]
▶ Could we do ”list comprehension” in TMP C++?▶ Yes, of course, but ...
Bartosz Kwaśniewski Functional Programming in C++ 115 / 131
![Page 485: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/485.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: haskell
▶ Count algorithm in Haskell▶ with recursion:
count1 [] = 0count1 (head:tail) = 1 + count1 tail
▶ with list comprehension:count2 lst = sum2 [1 | x <- lst]
▶ Could we do ”list comprehension” in TMP C++?▶ Yes, of course, but ...
Bartosz Kwaśniewski Functional Programming in C++ 115 / 131
![Page 486: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/486.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: haskell
▶ Count algorithm in Haskell▶ with recursion:
count1 [] = 0count1 (head:tail) = 1 + count1 tail
▶ with list comprehension:
count2 lst = sum2 [1 | x <- lst]
▶ Could we do ”list comprehension” in TMP C++?▶ Yes, of course, but ...
Bartosz Kwaśniewski Functional Programming in C++ 115 / 131
![Page 487: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/487.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: haskell
▶ Count algorithm in Haskell▶ with recursion:
count1 [] = 0count1 (head:tail) = 1 + count1 tail
▶ with list comprehension:
count2 lst = sum2 [1 | x <- lst]
▶ Could we do ”list comprehension” in TMP C++?▶ Yes, of course, but ...
Bartosz Kwaśniewski Functional Programming in C++ 115 / 131
![Page 488: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/488.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: haskell
▶ Count algorithm in Haskell▶ with recursion:
count1 [] = 0count1 (head:tail) = 1 + count1 tail
▶ with list comprehension:count2 lst = sum2 [1 | x <- lst]
▶ Could we do ”list comprehension” in TMP C++?▶ Yes, of course, but ...
Bartosz Kwaśniewski Functional Programming in C++ 115 / 131
![Page 489: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/489.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: haskell
▶ Count algorithm in Haskell▶ with recursion:
count1 [] = 0count1 (head:tail) = 1 + count1 tail
▶ with list comprehension:count2 lst = sum2 [1 | x <- lst]
▶ Could we do ”list comprehension” in TMP C++?
▶ Yes, of course, but ...
Bartosz Kwaśniewski Functional Programming in C++ 115 / 131
![Page 490: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/490.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: haskell
▶ Count algorithm in Haskell▶ with recursion:
count1 [] = 0count1 (head:tail) = 1 + count1 tail
▶ with list comprehension:count2 lst = sum2 [1 | x <- lst]
▶ Could we do ”list comprehension” in TMP C++?▶ Yes, of course, but ...
Bartosz Kwaśniewski Functional Programming in C++ 115 / 131
![Page 491: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/491.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: TMP C++
▶ Count algorithm in C++
▶ with recursion:
template <class... list> struct count;template <> struct count<> {
static const int value = 0;};template <class head, class... tail> struct count<head, tail...> {
static const int value = 1 + count<tail...>::value;};cout << count1<Int<1>, Int<4>, Char<'a'>>::value; //prints 3
Bartosz Kwaśniewski Functional Programming in C++ 116 / 131
![Page 492: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/492.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: TMP C++
▶ Count algorithm in C++▶ with recursion:
template <class... list> struct count;template <> struct count<> {
static const int value = 0;};template <class head, class... tail> struct count<head, tail...> {
static const int value = 1 + count<tail...>::value;};cout << count1<Int<1>, Int<4>, Char<'a'>>::value; //prints 3
Bartosz Kwaśniewski Functional Programming in C++ 116 / 131
![Page 493: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/493.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: TMP C++
▶ Count algorithm in C++▶ with recursion:
template <class... list> struct count;template <> struct count<> {
static const int value = 0;};template <class head, class... tail> struct count<head, tail...> {
static const int value = 1 + count<tail...>::value;};cout << count1<Int<1>, Int<4>, Char<'a'>>::value; //prints 3
Bartosz Kwaśniewski Functional Programming in C++ 116 / 131
![Page 494: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/494.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: List Comprehension in TMP C++▶ Count algorithm in C++ with list comprehension:
template <class> struct one { static const int value = 1; };template<class... lst> struct count {
static const int value = sum<one<lst>::value...>::value;};cout << count<Int<1>, Int<4>, Char<'a'>>::value
▶ How many expansions are there?sum<one<Int<1>>::value, one<Int<4>>::value, one<Char<'a'>>>.
▶ Just one, it is a one-to-one paradigm match
▶ There is sizeof... operator, but...
▶ it is Just sizeof, and...▶ The TMP list comprehension is applicable to any
transforming structure
Bartosz Kwaśniewski Functional Programming in C++ 117 / 131
![Page 495: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/495.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: List Comprehension in TMP C++▶ Count algorithm in C++ with list comprehension:
template <class> struct one { static const int value = 1; };template<class... lst> struct count {
static const int value = sum<one<lst>::value...>::value;};cout << count<Int<1>, Int<4>, Char<'a'>>::value
▶ How many expansions are there?
sum<one<Int<1>>::value, one<Int<4>>::value, one<Char<'a'>>>.
▶ Just one, it is a one-to-one paradigm match
▶ There is sizeof... operator, but...
▶ it is Just sizeof, and...▶ The TMP list comprehension is applicable to any
transforming structure
Bartosz Kwaśniewski Functional Programming in C++ 117 / 131
![Page 496: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/496.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: List Comprehension in TMP C++▶ Count algorithm in C++ with list comprehension:
template <class> struct one { static const int value = 1; };template<class... lst> struct count {
static const int value = sum<one<lst>::value...>::value;};cout << count<Int<1>, Int<4>, Char<'a'>>::value
▶ How many expansions are there?
sum<one<Int<1>>::value, one<Int<4>>::value, one<Char<'a'>>>.
▶ Just one, it is a one-to-one paradigm match▶ There is sizeof... operator, but...
▶ it is Just sizeof, and...▶ The TMP list comprehension is applicable to any
transforming structure
Bartosz Kwaśniewski Functional Programming in C++ 117 / 131
![Page 497: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/497.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: List Comprehension in TMP C++▶ Count algorithm in C++ with list comprehension:
template <class> struct one { static const int value = 1; };template<class... lst> struct count {
static const int value = sum<one<lst>::value...>::value;};cout << count<Int<1>, Int<4>, Char<'a'>>::value
▶ How many expansions are there?sum<one<Int<1>>::value, one<Int<4>>::value, one<Char<'a'>>>.
▶ Just one, it is a one-to-one paradigm match▶ There is sizeof... operator, but...
▶ it is Just sizeof, and...▶ The TMP list comprehension is applicable to any
transforming structure
Bartosz Kwaśniewski Functional Programming in C++ 117 / 131
![Page 498: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/498.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: List Comprehension in TMP C++▶ Count algorithm in C++ with list comprehension:
template <class> struct one { static const int value = 1; };template<class... lst> struct count {
static const int value = sum<one<lst>::value...>::value;};cout << count<Int<1>, Int<4>, Char<'a'>>::value
▶ How many expansions are there?sum<one<Int<1>>::value, one<Int<4>>::value, one<Char<'a'>>>.
▶ Just one, it is a one-to-one paradigm match
▶ There is sizeof... operator, but...
▶ it is Just sizeof, and...▶ The TMP list comprehension is applicable to any
transforming structure
Bartosz Kwaśniewski Functional Programming in C++ 117 / 131
![Page 499: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/499.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: List Comprehension in TMP C++▶ Count algorithm in C++ with list comprehension:
template <class> struct one { static const int value = 1; };template<class... lst> struct count {
static const int value = sum<one<lst>::value...>::value;};cout << count<Int<1>, Int<4>, Char<'a'>>::value
▶ How many expansions are there?sum<one<Int<1>>::value, one<Int<4>>::value, one<Char<'a'>>>.
▶ Just one, it is a one-to-one paradigm match▶ There is sizeof... operator, but...
▶ it is Just sizeof, and...▶ The TMP list comprehension is applicable to any
transforming structure
Bartosz Kwaśniewski Functional Programming in C++ 117 / 131
![Page 500: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/500.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: List Comprehension in TMP C++▶ Count algorithm in C++ with list comprehension:
template <class> struct one { static const int value = 1; };template<class... lst> struct count {
static const int value = sum<one<lst>::value...>::value;};cout << count<Int<1>, Int<4>, Char<'a'>>::value
▶ How many expansions are there?sum<one<Int<1>>::value, one<Int<4>>::value, one<Char<'a'>>>.
▶ Just one, it is a one-to-one paradigm match▶ There is sizeof... operator, but...
▶ it is Just sizeof, and...
▶ The TMP list comprehension is applicable to anytransforming structure
Bartosz Kwaśniewski Functional Programming in C++ 117 / 131
![Page 501: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/501.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
count: List Comprehension in TMP C++▶ Count algorithm in C++ with list comprehension:
template <class> struct one { static const int value = 1; };template<class... lst> struct count {
static const int value = sum<one<lst>::value...>::value;};cout << count<Int<1>, Int<4>, Char<'a'>>::value
▶ How many expansions are there?sum<one<Int<1>>::value, one<Int<4>>::value, one<Char<'a'>>>.
▶ Just one, it is a one-to-one paradigm match▶ There is sizeof... operator, but...
▶ it is Just sizeof, and...▶ The TMP list comprehension is applicable to any
transforming structureBartosz Kwaśniewski Functional Programming in C++ 117 / 131
![Page 502: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/502.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
map: haskell
▶ map and map with continuation
--continuationsum2 [] = 0sum2 (head:tail) = head + (sum2 tail)
--mapsmap f lst = [f x | x <- lst]mapcont cont f lst = cont [f x | x <- lst]
--transformationdoubleme x = x + x
--applicationprint(map doubleme [1,2,3]) --prints 2,4,6print(mapcont sum doubleme [1,2,3]) --prints 12
Bartosz Kwaśniewski Functional Programming in C++ 118 / 131
![Page 503: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/503.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
map: haskell
▶ map and map with continuation
--continuationsum2 [] = 0sum2 (head:tail) = head + (sum2 tail)
--mapsmap f lst = [f x | x <- lst]mapcont cont f lst = cont [f x | x <- lst]
--transformationdoubleme x = x + x
--applicationprint(map doubleme [1,2,3]) --prints 2,4,6print(mapcont sum doubleme [1,2,3]) --prints 12
Bartosz Kwaśniewski Functional Programming in C++ 118 / 131
![Page 504: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/504.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
map with continuation: TMP C++
▶ The map algorithm equivalence in C++
//continuationtemplate <class... Ts> struct Sum;template <> struct Sum<> {static const int value = 0;};template <class head, class... tail> struct Sum<head, tail...> {
static const int value = head::value + Sum<tail...>::value;};//transformationtemplate <class T> struct Twice {
static const int value = T::value + T::value;};//map with continuationtemplate <template <class...> class Cont,template <class> class F, class... Ts> struct Map {
static const int value = Cont<F<Ts>...>::value;};//applicationcout << Map<Sum, Twice, Int<1>, Int<2>, Int<3>>::value; //prints 12
Bartosz Kwaśniewski Functional Programming in C++ 119 / 131
![Page 505: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/505.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
map with continuation: TMP C++
▶ The map algorithm equivalence in C++
//continuationtemplate <class... Ts> struct Sum;template <> struct Sum<> {static const int value = 0;};template <class head, class... tail> struct Sum<head, tail...> {
static const int value = head::value + Sum<tail...>::value;};//transformationtemplate <class T> struct Twice {
static const int value = T::value + T::value;};//map with continuationtemplate <template <class...> class Cont,template <class> class F, class... Ts> struct Map {
static const int value = Cont<F<Ts>...>::value;};//applicationcout << Map<Sum, Twice, Int<1>, Int<2>, Int<3>>::value; //prints 12
Bartosz Kwaśniewski Functional Programming in C++ 119 / 131
![Page 506: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/506.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Functional Metaprogramming Libraries
▶ Boost.MPL▶ Template metaprogramming framework of compile-time
algorithms, sequences and metafunctions.
▶ Boost.Fusion
▶ Heterogeneous containers with algorithms
▶ Boost.Hana
▶ Header-only library for C++ metaprogramming suitedfor computations on both types and values
▶ Range v3
▶ Experimental range library for C++11/14/17 with rangecomprehension
Bartosz Kwaśniewski Functional Programming in C++ 120 / 131
![Page 507: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/507.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Functional Metaprogramming Libraries
▶ Boost.MPL▶ Template metaprogramming framework of compile-time
algorithms, sequences and metafunctions.▶ Boost.Fusion
▶ Heterogeneous containers with algorithms
▶ Boost.Hana
▶ Header-only library for C++ metaprogramming suitedfor computations on both types and values
▶ Range v3
▶ Experimental range library for C++11/14/17 with rangecomprehension
Bartosz Kwaśniewski Functional Programming in C++ 120 / 131
![Page 508: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/508.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Functional Metaprogramming Libraries
▶ Boost.MPL▶ Template metaprogramming framework of compile-time
algorithms, sequences and metafunctions.▶ Boost.Fusion
▶ Heterogeneous containers with algorithms▶ Boost.Hana
▶ Header-only library for C++ metaprogramming suitedfor computations on both types and values
▶ Range v3
▶ Experimental range library for C++11/14/17 with rangecomprehension
Bartosz Kwaśniewski Functional Programming in C++ 120 / 131
![Page 509: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/509.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
C++ Functional Metaprogramming Libraries
▶ Boost.MPL▶ Template metaprogramming framework of compile-time
algorithms, sequences and metafunctions.▶ Boost.Fusion
▶ Heterogeneous containers with algorithms▶ Boost.Hana
▶ Header-only library for C++ metaprogramming suitedfor computations on both types and values
▶ Range v3▶ Experimental range library for C++11/14/17 with range
comprehension
Bartosz Kwaśniewski Functional Programming in C++ 120 / 131
![Page 510: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/510.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Monad:Flatten, MonadPlus:Filter
▶ Monad::Flatten
//flatten - collapse two levels of monadic structure into a single levelstatic_assert(hana::flatten(hana::make_tuple(hana::make_tuple(1, 2, 3),
hana::make_tuple(4, 5),hana::make_tuple(6, 7, 8, 9)))==hana::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9), "");
▶ Monad::Filter
//filter - filter a monadic structure using a custom predicatestatic_assert(hana::filter(hana::make_tuple(1, 2.0, 3, 4.0), is_integral)==hana::make_tuple(1, 3), "");
Bartosz Kwaśniewski Functional Programming in C++ 121 / 131
![Page 511: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/511.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Monad:Flatten, MonadPlus:Filter
▶ Monad::Flatten//flatten - collapse two levels of monadic structure into a single levelstatic_assert(hana::flatten(hana::make_tuple(hana::make_tuple(1, 2, 3),
hana::make_tuple(4, 5),hana::make_tuple(6, 7, 8, 9)))==hana::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9), "");
▶ Monad::Filter//filter - filter a monadic structure using a custom predicatestatic_assert(hana::filter(hana::make_tuple(1, 2.0, 3, 4.0), is_integral)==hana::make_tuple(1, 3), "");
Bartosz Kwaśniewski Functional Programming in C++ 121 / 131
![Page 512: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/512.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Monad:Flatten, MonadPlus:Filter
▶ Monad::Flatten//flatten - collapse two levels of monadic structure into a single levelstatic_assert(hana::flatten(hana::make_tuple(hana::make_tuple(1, 2, 3),
hana::make_tuple(4, 5),hana::make_tuple(6, 7, 8, 9)))==hana::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9), "");
▶ Monad::Filter
//filter - filter a monadic structure using a custom predicatestatic_assert(hana::filter(hana::make_tuple(1, 2.0, 3, 4.0), is_integral)==hana::make_tuple(1, 3), "");
Bartosz Kwaśniewski Functional Programming in C++ 121 / 131
![Page 513: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/513.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Monad:Flatten, MonadPlus:Filter
▶ Monad::Flatten//flatten - collapse two levels of monadic structure into a single levelstatic_assert(hana::flatten(hana::make_tuple(hana::make_tuple(1, 2, 3),
hana::make_tuple(4, 5),hana::make_tuple(6, 7, 8, 9)))==hana::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9), "");
▶ Monad::Filter//filter - filter a monadic structure using a custom predicatestatic_assert(hana::filter(hana::make_tuple(1, 2.0, 3, 4.0), is_integral)==hana::make_tuple(1, 3), "");
Bartosz Kwaśniewski Functional Programming in C++ 121 / 131
![Page 514: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/514.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Sequence: Zip,Partition▶ Sequence::Zip
//zip_shortest_with - zip one sequence or more with a given functionstatic_assert(
hana::zip_shortest_with(hana::mult,hana::make_tuple(1, 2, 3, 4), hana::make_tuple(5, 6, 7, 8, "ignored"))== hana::make_tuple(5, 12, 21, 32), "");
▶ Sequence::Partition
//partition - Partition a sequence based on a predicateBOOST_HANA_CONSTANT_CHECK(
hana::partition(hana::tuple_c<int, 1, 2, 3, 4, 5, 6, 7>, [](auto x) {return x % hana::int_c<2> != hana::int_c<0>;
}) ==hana::make_pair(
hana::tuple_c<int, 1, 3, 5, 7>,hana::tuple_c<int, 2, 4, 6>
));
Bartosz Kwaśniewski Functional Programming in C++ 122 / 131
![Page 515: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/515.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Sequence: Zip,Partition▶ Sequence::Zip//zip_shortest_with - zip one sequence or more with a given functionstatic_assert(
hana::zip_shortest_with(hana::mult,hana::make_tuple(1, 2, 3, 4), hana::make_tuple(5, 6, 7, 8, "ignored"))== hana::make_tuple(5, 12, 21, 32), "");
▶ Sequence::Partition//partition - Partition a sequence based on a predicateBOOST_HANA_CONSTANT_CHECK(
hana::partition(hana::tuple_c<int, 1, 2, 3, 4, 5, 6, 7>, [](auto x) {return x % hana::int_c<2> != hana::int_c<0>;
}) ==hana::make_pair(
hana::tuple_c<int, 1, 3, 5, 7>,hana::tuple_c<int, 2, 4, 6>
));
Bartosz Kwaśniewski Functional Programming in C++ 122 / 131
![Page 516: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/516.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Sequence: Zip,Partition▶ Sequence::Zip//zip_shortest_with - zip one sequence or more with a given functionstatic_assert(
hana::zip_shortest_with(hana::mult,hana::make_tuple(1, 2, 3, 4), hana::make_tuple(5, 6, 7, 8, "ignored"))== hana::make_tuple(5, 12, 21, 32), "");
▶ Sequence::Partition
//partition - Partition a sequence based on a predicateBOOST_HANA_CONSTANT_CHECK(
hana::partition(hana::tuple_c<int, 1, 2, 3, 4, 5, 6, 7>, [](auto x) {return x % hana::int_c<2> != hana::int_c<0>;
}) ==hana::make_pair(
hana::tuple_c<int, 1, 3, 5, 7>,hana::tuple_c<int, 2, 4, 6>
));
Bartosz Kwaśniewski Functional Programming in C++ 122 / 131
![Page 517: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/517.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Sequence: Zip,Partition▶ Sequence::Zip//zip_shortest_with - zip one sequence or more with a given functionstatic_assert(
hana::zip_shortest_with(hana::mult,hana::make_tuple(1, 2, 3, 4), hana::make_tuple(5, 6, 7, 8, "ignored"))== hana::make_tuple(5, 12, 21, 32), "");
▶ Sequence::Partition//partition - Partition a sequence based on a predicateBOOST_HANA_CONSTANT_CHECK(
hana::partition(hana::tuple_c<int, 1, 2, 3, 4, 5, 6, 7>, [](auto x) {return x % hana::int_c<2> != hana::int_c<0>;
}) ==hana::make_pair(
hana::tuple_c<int, 1, 3, 5, 7>,hana::tuple_c<int, 2, 4, 6>
));
Bartosz Kwaśniewski Functional Programming in C++ 122 / 131
![Page 518: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/518.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Functional: partial, curry, compose▶ Functional::partial
//partial - Partially apply a function to some argumentsconstexpr auto increment = hana::partial(hana::plus, 1);static_assert(increment(2) == 3, "");
▶ Functional::curry
//curry - curry a function up to the given number of argumentshana::curry<3>(add)(1)(2)(3) == 1 + 2 + 3);hana::curry<3>(add)(1, 2, 3) == hana::curry<3>(add)(1)(2)(3));
▶ Functional::compose
//compose - return the composition of two functions or morehana::compose(to_char, increment)(3) == '4');
Bartosz Kwaśniewski Functional Programming in C++ 123 / 131
![Page 519: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/519.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Functional: partial, curry, compose▶ Functional::partial//partial - Partially apply a function to some argumentsconstexpr auto increment = hana::partial(hana::plus, 1);static_assert(increment(2) == 3, "");
▶ Functional::curry//curry - curry a function up to the given number of argumentshana::curry<3>(add)(1)(2)(3) == 1 + 2 + 3);hana::curry<3>(add)(1, 2, 3) == hana::curry<3>(add)(1)(2)(3));
▶ Functional::compose
//compose - return the composition of two functions or morehana::compose(to_char, increment)(3) == '4');
Bartosz Kwaśniewski Functional Programming in C++ 123 / 131
![Page 520: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/520.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Functional: partial, curry, compose▶ Functional::partial//partial - Partially apply a function to some argumentsconstexpr auto increment = hana::partial(hana::plus, 1);static_assert(increment(2) == 3, "");
▶ Functional::curry
//curry - curry a function up to the given number of argumentshana::curry<3>(add)(1)(2)(3) == 1 + 2 + 3);hana::curry<3>(add)(1, 2, 3) == hana::curry<3>(add)(1)(2)(3));
▶ Functional::compose
//compose - return the composition of two functions or morehana::compose(to_char, increment)(3) == '4');
Bartosz Kwaśniewski Functional Programming in C++ 123 / 131
![Page 521: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/521.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Functional: partial, curry, compose▶ Functional::partial//partial - Partially apply a function to some argumentsconstexpr auto increment = hana::partial(hana::plus, 1);static_assert(increment(2) == 3, "");
▶ Functional::curry//curry - curry a function up to the given number of argumentshana::curry<3>(add)(1)(2)(3) == 1 + 2 + 3);hana::curry<3>(add)(1, 2, 3) == hana::curry<3>(add)(1)(2)(3));
▶ Functional::compose//compose - return the composition of two functions or morehana::compose(to_char, increment)(3) == '4');
Bartosz Kwaśniewski Functional Programming in C++ 123 / 131
![Page 522: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/522.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Functional: partial, curry, compose▶ Functional::partial//partial - Partially apply a function to some argumentsconstexpr auto increment = hana::partial(hana::plus, 1);static_assert(increment(2) == 3, "");
▶ Functional::curry//curry - curry a function up to the given number of argumentshana::curry<3>(add)(1)(2)(3) == 1 + 2 + 3);hana::curry<3>(add)(1, 2, 3) == hana::curry<3>(add)(1)(2)(3));
▶ Functional::compose
//compose - return the composition of two functions or morehana::compose(to_char, increment)(3) == '4');
Bartosz Kwaśniewski Functional Programming in C++ 123 / 131
![Page 523: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/523.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Hana: Functional: partial, curry, compose▶ Functional::partial//partial - Partially apply a function to some argumentsconstexpr auto increment = hana::partial(hana::plus, 1);static_assert(increment(2) == 3, "");
▶ Functional::curry//curry - curry a function up to the given number of argumentshana::curry<3>(add)(1)(2)(3) == 1 + 2 + 3);hana::curry<3>(add)(1, 2, 3) == hana::curry<3>(add)(1)(2)(3));
▶ Functional::compose//compose - return the composition of two functions or morehana::compose(to_char, increment)(3) == '4');
Bartosz Kwaśniewski Functional Programming in C++ 123 / 131
![Page 524: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/524.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:
▶ not tersed: one liner became ten liners▶ structures acts as functions▶ template specializations acts as partial matching and
recursions▶ operates on template parameters, mostly on types▶ only integers are supported▶ no lambdas and nested functions▶ compile time only▶ lacking of built in combinators
▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 525: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/525.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners
▶ structures acts as functions▶ template specializations acts as partial matching and
recursions▶ operates on template parameters, mostly on types▶ only integers are supported▶ no lambdas and nested functions▶ compile time only▶ lacking of built in combinators
▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 526: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/526.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners▶ structures acts as functions
▶ template specializations acts as partial matching andrecursions
▶ operates on template parameters, mostly on types▶ only integers are supported▶ no lambdas and nested functions▶ compile time only▶ lacking of built in combinators
▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 527: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/527.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners▶ structures acts as functions▶ template specializations acts as partial matching and
recursions
▶ operates on template parameters, mostly on types▶ only integers are supported▶ no lambdas and nested functions▶ compile time only▶ lacking of built in combinators
▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 528: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/528.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners▶ structures acts as functions▶ template specializations acts as partial matching and
recursions▶ operates on template parameters, mostly on types
▶ only integers are supported▶ no lambdas and nested functions▶ compile time only▶ lacking of built in combinators
▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 529: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/529.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners▶ structures acts as functions▶ template specializations acts as partial matching and
recursions▶ operates on template parameters, mostly on types▶ only integers are supported
▶ no lambdas and nested functions▶ compile time only▶ lacking of built in combinators
▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 530: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/530.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners▶ structures acts as functions▶ template specializations acts as partial matching and
recursions▶ operates on template parameters, mostly on types▶ only integers are supported▶ no lambdas and nested functions
▶ compile time only▶ lacking of built in combinators
▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 531: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/531.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners▶ structures acts as functions▶ template specializations acts as partial matching and
recursions▶ operates on template parameters, mostly on types▶ only integers are supported▶ no lambdas and nested functions▶ compile time only
▶ lacking of built in combinators▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 532: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/532.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners▶ structures acts as functions▶ template specializations acts as partial matching and
recursions▶ operates on template parameters, mostly on types▶ only integers are supported▶ no lambdas and nested functions▶ compile time only▶ lacking of built in combinators
▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 533: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/533.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners▶ structures acts as functions▶ template specializations acts as partial matching and
recursions▶ operates on template parameters, mostly on types▶ only integers are supported▶ no lambdas and nested functions▶ compile time only▶ lacking of built in combinators
▶ However:
▶ There is Boost.Hana that greatly simplify thefunctional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 534: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/534.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
C++ Metaprogramming as Functional Language
Metaprogramming Summary: Key Points
▶ Some support, but not easy:▶ not tersed: one liner became ten liners▶ structures acts as functions▶ template specializations acts as partial matching and
recursions▶ operates on template parameters, mostly on types▶ only integers are supported▶ no lambdas and nested functions▶ compile time only▶ lacking of built in combinators
▶ However:▶ There is Boost.Hana that greatly simplify the
functional metaprogramming
Bartosz Kwaśniewski Functional Programming in C++ 124 / 131
![Page 535: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/535.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work, Conclusion, Recommendation
Future Work, Conclusion, Recommendation
Bartosz Kwaśniewski Functional Programming in C++ 125 / 131
![Page 536: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/536.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work: Design Consideration▶ What is the influence of functional paradigm shift
on architecture?
▶ Where is the biggest impact: in the large, medium andthe small level?
▶ Does OOD and FP are orthogonal?
▶ Is one concept or paradigm does not either imply or excludethe other?
▶ Could we use both to make better programs (cleaner, morebeauty)?
▶ Does architecture smells (rigidity, fragility, immobility)and its remedies, the SOLID principles are affected?
▶ What about design patterns?
▶ Maybe some design patterns are NOT needed any more orcould be simplified?
▶ Does language composability capabilities areincreased?
Bartosz Kwaśniewski Functional Programming in C++ 126 / 131
![Page 537: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/537.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work: Design Consideration▶ What is the influence of functional paradigm shift
on architecture?▶ Where is the biggest impact: in the large, medium and
the small level?
▶ Does OOD and FP are orthogonal?
▶ Is one concept or paradigm does not either imply or excludethe other?
▶ Could we use both to make better programs (cleaner, morebeauty)?
▶ Does architecture smells (rigidity, fragility, immobility)and its remedies, the SOLID principles are affected?
▶ What about design patterns?
▶ Maybe some design patterns are NOT needed any more orcould be simplified?
▶ Does language composability capabilities areincreased?
Bartosz Kwaśniewski Functional Programming in C++ 126 / 131
![Page 538: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/538.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work: Design Consideration▶ What is the influence of functional paradigm shift
on architecture?▶ Where is the biggest impact: in the large, medium and
the small level?▶ Does OOD and FP are orthogonal?
▶ Is one concept or paradigm does not either imply or excludethe other?
▶ Could we use both to make better programs (cleaner, morebeauty)?
▶ Does architecture smells (rigidity, fragility, immobility)and its remedies, the SOLID principles are affected?
▶ What about design patterns?
▶ Maybe some design patterns are NOT needed any more orcould be simplified?
▶ Does language composability capabilities areincreased?
Bartosz Kwaśniewski Functional Programming in C++ 126 / 131
![Page 539: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/539.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work: Design Consideration▶ What is the influence of functional paradigm shift
on architecture?▶ Where is the biggest impact: in the large, medium and
the small level?▶ Does OOD and FP are orthogonal?
▶ Is one concept or paradigm does not either imply or excludethe other?
▶ Could we use both to make better programs (cleaner, morebeauty)?
▶ Does architecture smells (rigidity, fragility, immobility)and its remedies, the SOLID principles are affected?
▶ What about design patterns?
▶ Maybe some design patterns are NOT needed any more orcould be simplified?
▶ Does language composability capabilities areincreased?
Bartosz Kwaśniewski Functional Programming in C++ 126 / 131
![Page 540: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/540.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work: Design Consideration▶ What is the influence of functional paradigm shift
on architecture?▶ Where is the biggest impact: in the large, medium and
the small level?▶ Does OOD and FP are orthogonal?
▶ Is one concept or paradigm does not either imply or excludethe other?
▶ Could we use both to make better programs (cleaner, morebeauty)?
▶ Does architecture smells (rigidity, fragility, immobility)and its remedies, the SOLID principles are affected?
▶ What about design patterns?
▶ Maybe some design patterns are NOT needed any more orcould be simplified?
▶ Does language composability capabilities areincreased?
Bartosz Kwaśniewski Functional Programming in C++ 126 / 131
![Page 541: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/541.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work: Design Consideration▶ What is the influence of functional paradigm shift
on architecture?▶ Where is the biggest impact: in the large, medium and
the small level?▶ Does OOD and FP are orthogonal?
▶ Is one concept or paradigm does not either imply or excludethe other?
▶ Could we use both to make better programs (cleaner, morebeauty)?
▶ Does architecture smells (rigidity, fragility, immobility)and its remedies, the SOLID principles are affected?
▶ What about design patterns?
▶ Maybe some design patterns are NOT needed any more orcould be simplified?
▶ Does language composability capabilities areincreased?
Bartosz Kwaśniewski Functional Programming in C++ 126 / 131
![Page 542: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/542.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work: Design Consideration▶ What is the influence of functional paradigm shift
on architecture?▶ Where is the biggest impact: in the large, medium and
the small level?▶ Does OOD and FP are orthogonal?
▶ Is one concept or paradigm does not either imply or excludethe other?
▶ Could we use both to make better programs (cleaner, morebeauty)?
▶ Does architecture smells (rigidity, fragility, immobility)and its remedies, the SOLID principles are affected?
▶ What about design patterns?
▶ Maybe some design patterns are NOT needed any more orcould be simplified?
▶ Does language composability capabilities areincreased?
Bartosz Kwaśniewski Functional Programming in C++ 126 / 131
![Page 543: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/543.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work: Design Consideration▶ What is the influence of functional paradigm shift
on architecture?▶ Where is the biggest impact: in the large, medium and
the small level?▶ Does OOD and FP are orthogonal?
▶ Is one concept or paradigm does not either imply or excludethe other?
▶ Could we use both to make better programs (cleaner, morebeauty)?
▶ Does architecture smells (rigidity, fragility, immobility)and its remedies, the SOLID principles are affected?
▶ What about design patterns?▶ Maybe some design patterns are NOT needed any more or
could be simplified?
▶ Does language composability capabilities areincreased?
Bartosz Kwaśniewski Functional Programming in C++ 126 / 131
![Page 544: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/544.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Future Work: Design Consideration▶ What is the influence of functional paradigm shift
on architecture?▶ Where is the biggest impact: in the large, medium and
the small level?▶ Does OOD and FP are orthogonal?
▶ Is one concept or paradigm does not either imply or excludethe other?
▶ Could we use both to make better programs (cleaner, morebeauty)?
▶ Does architecture smells (rigidity, fragility, immobility)and its remedies, the SOLID principles are affected?
▶ What about design patterns?▶ Maybe some design patterns are NOT needed any more or
could be simplified?▶ Does language composability capabilities are
increased?Bartosz Kwaśniewski Functional Programming in C++ 126 / 131
![Page 545: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/545.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Conclusion
▶ Is a C++ a functional language?
▶ No. It is an imperative language with some functionalconcepts.
▶ Yes. The metaprogramming is the compile-time purefunctional language.
▶ Does a paradigm shift in C++ in a FP direction is agood thing?
▶ Yes, open more opportunities for writing better software,but... its harder from developer pov ;)
Bartosz Kwaśniewski Functional Programming in C++ 127 / 131
![Page 546: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/546.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Conclusion
▶ Is a C++ a functional language?▶ No. It is an imperative language with some functional
concepts.
▶ Yes. The metaprogramming is the compile-time purefunctional language.
▶ Does a paradigm shift in C++ in a FP direction is agood thing?
▶ Yes, open more opportunities for writing better software,but... its harder from developer pov ;)
Bartosz Kwaśniewski Functional Programming in C++ 127 / 131
![Page 547: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/547.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Conclusion
▶ Is a C++ a functional language?▶ No. It is an imperative language with some functional
concepts.▶ Yes. The metaprogramming is the compile-time pure
functional language.
▶ Does a paradigm shift in C++ in a FP direction is agood thing?
▶ Yes, open more opportunities for writing better software,but... its harder from developer pov ;)
Bartosz Kwaśniewski Functional Programming in C++ 127 / 131
![Page 548: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/548.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Conclusion
▶ Is a C++ a functional language?▶ No. It is an imperative language with some functional
concepts.▶ Yes. The metaprogramming is the compile-time pure
functional language.▶ Does a paradigm shift in C++ in a FP direction is agood thing?
▶ Yes, open more opportunities for writing better software,but... its harder from developer pov ;)
Bartosz Kwaśniewski Functional Programming in C++ 127 / 131
![Page 549: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/549.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Conclusion
▶ Is a C++ a functional language?▶ No. It is an imperative language with some functional
concepts.▶ Yes. The metaprogramming is the compile-time pure
functional language.▶ Does a paradigm shift in C++ in a FP direction is agood thing?
▶ Yes, open more opportunities for writing better software,but... its harder from developer pov ;)
Bartosz Kwaśniewski Functional Programming in C++ 127 / 131
![Page 550: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/550.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Recommendation
▶ Use whatever concepts satisfy your need
▶ Just have a fun with programming▶ Learn and practice functional programming bydoing haskell
▶ Sorry, I couldn’t resists ...▶ C++ is more functional then JavaScript▶ Even there is a book “Functional JavaScript”▶ I’m (We’re) eagerly awaiting for “Functional
C++”
Bartosz Kwaśniewski Functional Programming in C++ 128 / 131
![Page 551: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/551.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Recommendation
▶ Use whatever concepts satisfy your need▶ Just have a fun with programming
▶ Learn and practice functional programming bydoing haskell
▶ Sorry, I couldn’t resists ...▶ C++ is more functional then JavaScript▶ Even there is a book “Functional JavaScript”▶ I’m (We’re) eagerly awaiting for “Functional
C++”
Bartosz Kwaśniewski Functional Programming in C++ 128 / 131
![Page 552: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/552.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Recommendation
▶ Use whatever concepts satisfy your need▶ Just have a fun with programming▶ Learn and practice functional programming bydoing haskell
▶ Sorry, I couldn’t resists ...▶ C++ is more functional then JavaScript▶ Even there is a book “Functional JavaScript”▶ I’m (We’re) eagerly awaiting for “Functional
C++”
Bartosz Kwaśniewski Functional Programming in C++ 128 / 131
![Page 553: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/553.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Recommendation
▶ Use whatever concepts satisfy your need▶ Just have a fun with programming▶ Learn and practice functional programming bydoing haskell
▶ Sorry, I couldn’t resists ...
▶ C++ is more functional then JavaScript▶ Even there is a book “Functional JavaScript”▶ I’m (We’re) eagerly awaiting for “Functional
C++”
Bartosz Kwaśniewski Functional Programming in C++ 128 / 131
![Page 554: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/554.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Recommendation
▶ Use whatever concepts satisfy your need▶ Just have a fun with programming▶ Learn and practice functional programming bydoing haskell
▶ Sorry, I couldn’t resists ...▶ C++ is more functional then JavaScript
▶ Even there is a book “Functional JavaScript”▶ I’m (We’re) eagerly awaiting for “Functional
C++”
Bartosz Kwaśniewski Functional Programming in C++ 128 / 131
![Page 555: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/555.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Recommendation
▶ Use whatever concepts satisfy your need▶ Just have a fun with programming▶ Learn and practice functional programming bydoing haskell
▶ Sorry, I couldn’t resists ...▶ C++ is more functional then JavaScript▶ Even there is a book “Functional JavaScript”
▶ I’m (We’re) eagerly awaiting for “FunctionalC++”
Bartosz Kwaśniewski Functional Programming in C++ 128 / 131
![Page 556: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/556.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Recommendation
▶ Use whatever concepts satisfy your need▶ Just have a fun with programming▶ Learn and practice functional programming bydoing haskell
▶ Sorry, I couldn’t resists ...▶ C++ is more functional then JavaScript▶ Even there is a book “Functional JavaScript”▶ I’m (We’re) eagerly awaiting for “Functional
C++”
Bartosz Kwaśniewski Functional Programming in C++ 128 / 131
![Page 557: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/557.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Open Questions
▶ How does the creators of C++ understand thefunctional paradigm?
▶ Where the functional paradigm shift will take aC++ language?
▶ Is there a clear vision behind the evolution ofC++?
Bartosz Kwaśniewski Functional Programming in C++ 129 / 131
![Page 558: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/558.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Open Questions
▶ How does the creators of C++ understand thefunctional paradigm?
▶ Where the functional paradigm shift will take aC++ language?
▶ Is there a clear vision behind the evolution ofC++?
Bartosz Kwaśniewski Functional Programming in C++ 129 / 131
![Page 559: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/559.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
Open Questions
▶ How does the creators of C++ understand thefunctional paradigm?
▶ Where the functional paradigm shift will take aC++ language?
▶ Is there a clear vision behind the evolution ofC++?
Bartosz Kwaśniewski Functional Programming in C++ 129 / 131
![Page 560: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/560.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Conclusion
The End
Thank You Very Much
Bartosz Kwaśniewski Functional Programming in C++ 130 / 131
![Page 561: Functional Programming in C++ - C++ Support for Functional ... · Functional Programming in C++ C++SupportforFunctionalParadigm BartoszKwaśniewski Bartosz Kwaśniewski Functional](https://reader033.fdocuments.net/reader033/viewer/2022052314/5c76de3109d3f2d3778c343d/html5/thumbnails/561.jpg)
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Bibliography
Bibliography
Why Functional programming Matters. John Hughes. 1990.
Bartosz Milewski’s Programming Cafe, bartoszmilewski.com
The C++ Programming Language 4th Edition, B. Stroustrup, 2013
C++ FAQ, isocpp.org/faq
A History of Haskell: Being Lazy With Class. Hudak, 2007
Learn You a Haskell for Great Good, Lipovaca, 2011
Boost.Hana User Manual
And many, many others ...
Bartosz Kwaśniewski Functional Programming in C++ 131 / 131