Please rotate your tablet to be horizontal.

You can dismiss this notice but please note that this experience has not been designed for mobile devices and so will be less than optimal

Back To Schedule

Functional Composable Operations with Unix-Style Pipes in C++

11:00 - 12:00 Monday 12th September 2022 MDT Summit 1 / Online E
Beginner
Intermediate
Advanced
Expert
Algorithms & Data Structures

Let's write C++ code like this shaking off all the verbosity and with 100% domain code and ~0% syntax! Do we really need to explicitly state whether a function is a continuation or a transform?

return get_env("KafkaConsumerConfigFile")
        | get_file_contents
        | make_client
        | connect
        | subscribe;

Unix commands seamlessly compose, piping into each other to form a clear train of operations in one line! Similarly in C++, we moved away from non-composable, multi-step processing of ranges using <algorithm>. We embraced the composable <ranges> using pipes. This reduced time & space complexity, improved performance, provided clarity & reduced cognitive load when working with ranges.

But, when NOT dealing with std::ranges, can one work with such easy composability? Why not pipe operations to work with application/business entities! YES, of course, I say! Let's pipe application logic operations & get declarative! HOW HARD COULD IT BE!

With just C++11, we achieve piping operations for any generic T & a Callable. We address Error handling, Resource management, Customizations and Variations like chaining functions taking more than 1 argument or invoking a member function operation instead of free function. Supporting C++20, we make sure our pipe implementation does NOT interfere with std::ranges operations.

Finally, we address the proposed C++23 Monadic interface functions (and_then, transform, or_else) of std::optional and present how we can implement the same without having to explicitly state the verbose and_then, transform, or_else to work with optional. We encounter a surprise on the way & correct overload resolution by employing a concept.

We also discuss critique & compromises of this approach.

Ankur Satle

ArchitectEXFO

Ankur is an avid (natural) language learner, but C++ is his mother tongue!
His mantra is: "Effective, Efficient & Elegant". He leverages the power of the small to build large-scale systems. As Smart Data Platform Architect at EXFO, he builds High-performance, Scalable, Telecom Products for 4G/5G Core. He loves working in constrained environments.
He was the Organizer & Host of CppIndiaCon, since inception. His dream came true when he hosted the AMA with Bjarne Stroustrup!
He is the Core Organizer at CppIndia, India's C++ User Group, the most active C++ user group.
He has presented at CppCon 2020 & CppCon 2021, CppIndiaCon 2021 & CppIndiaCon 2022