Functional Programming in Modern C++: The Imperatives Must Go! - Victor Ciura - ACCU 2023
Вставка
- Опубліковано 8 лют 2025
- ACCU Membership: tinyurl.com/yd...
accu.org
www.accuconfer...
Functional Programming in Modern C++: The Imperatives Must Go! - Victor Ciura - ACCU 2023
Slides: accu.org/conf-...
Can a language whose official motto is "Avoid Success at All Costs" teach us new tricks in modern C++ ?
If Haskell is so great, why hasn't it taken over the world? My claim is that it has. But not as a Roman legion loudly marching in a new territory, rather as distributed Trojan horses popping in at the gates, masquerading as modern features or novel ideas in today’s mainstream languages. Functional Programming ideas that have been around for over 40 years will be rediscovered to solve our current software complexity problems.
Indeed, modern C++ has become more functional. From mundane concepts like lambdas & closures, std::function, values types and constants, to composability of STL algorithms, lazy ranges, folding, mapping or even higher-order functions in STL. Did I mention Rust yet?
In this session we’ll analyze a bunch of FP techniques in C++ and see how they help make our code shorter, clearer and faster, by embracing a declarative vs. an imperative style. We’ll visit the functional parts of current STL, use algebraic data types (ADT) and learn about the new FP stuff coming in the next C++ standard, like ranges or monadic extensions to std::future, std::optional and std::expected. Brace yourselves for a bumpy ride including composition, lifting, currying, partial application, pure functions, maybe even pattern matching and lazy evaluation.
---
Victor Ciura
Victor Ciura is a Principal Engineer on the Visual C++ team, helping to improve the tools he’s been using for years. Before joining Microsoft, he programmed C++ professionally for 20 years, designing and implementing several core components & libraries of Advanced Installer, improving the virtualization and repackaging technologies for MSI/MSIX.
One of his hobbies is tidying-up and modernizing aging codebases and has been known to build open-source tools that help this process: Clang Power Tools.
He’s a regular guest at Computer Science Department of his Alma Mater, University of Craiova, where he gives student lectures & workshops on using modern C++, STL, algorithms and optimization techniques.
More details: @ciura_victor & ciura.ro & linkedin.com/in/victor-ciura
---
UA-cam Videos Filmed, Edited & Optimised by Digital Medium: events.digital...
#accuconf #programming #cpp
1:06:01 [slide 97] also - the imperative code on the left will not work (UB) if size(v) is smaller than 2, and the ranges-style will work.
1:10:24 [slides 102-102] note that ranges::sort is not currently pipeable in that way, I believe. One might need to call sort() on the pipe-expression instead.
I remember reading an article by John Carmack that mentioned knowing Functional Programming was probably a good thing, and I ended up following an FP based minor that also covered parsing and regex, all because of that comment.
Now I'm trying to not use it, as it tends to produce slower code then imperative. Though I'll use it, if the situation allows it.
1:24:50 I think tail can be defined as: “auto tail = [](const range auto&& r) { return views::all(forward(r)) | views::drop(1);};”.
I’m not sure about the usage of views::all and move-semantics here. I’m happy to see replies correcting me.
Would std::accumulate be considered FP when it replaces a for loop based summming? Won't std::accumulate have to do the same for loop within it's body? I don't understand how FP cannot be imperative at the base level through which functions like sum are created.
I never understood the point of that Knuth story. What does it show other than that if you have a language with more powerful special purpose primitives, your programs in that domain get shorter? Big woop.
When I first seen Haskel, I thought, what Alien invented this?
The Elephant in the room is that Pure Functional programming can't be achieved, everything has a side effect, or we wouldn't be manipulating code and Data. Data doesn't Run, Code runs to perform actions/work on Data. I personally prefer some Functions over creating NEW objects or Structs if I'm dealing with common data sets (like a Human input data, Name etc). In the end a Pure Functional program does nothing of value, because side effects IS displaying information to the User.