Book Image

Hands-On Functional Programming with C++

By : Alexandru Bolboaca
Book Image

Hands-On Functional Programming with C++

By: Alexandru Bolboaca

Overview of this book

Functional programming enables you to divide your software into smaller, reusable components that are easy to write, debug, and maintain. Combined with the power of C++, you can develop scalable and functional applications for modern software requirements. This book will help you discover the functional features in C++ 17 and C++ 20 to build enterprise-level applications. Starting with the fundamental building blocks of functional programming and how to use them in C++, you’ll explore functions, currying, and lambdas. As you advance, you’ll learn how to improve cohesion and delve into test-driven development, which will enable you in designing better software. In addition to this, the book covers architectural patterns such as event sourcing to help you get to grips with the importance of immutability for data storage. You’ll even understand how to “think in functions” and implement design patterns in a functional way. By the end of this book, you’ll be able to write faster and cleaner production code in C++ with the help of functional programming.
Table of Contents (23 chapters)
Free Chapter
1
Section 1: Functional Building Blocks in C++
7
Section 2: Design with Functions
12
Section 3: Reaping the Benefits of Functional Programming
17
Section 4: The Present and Future of Functional Programming in C++

Preface

Welcome to a hands-on tour of functional programming in C++! This book is about an old idea, that is, functional programming, and a classic programming language, that is, C++, finally uniting forces.

Functional programming has been around since the 1950s; however, due to its mathematical underpinnings, it has been of limited interest to mainstream software development for many years. With the advent of multicore CPUs and big data leading to the need for parallelization, and with programming language designers becoming more interested in immutability and lambdas, functional programming concepts have been gradually introduced in all major programming languages, including C#, Java, PHP, JavaScript, Python, and Ruby. C++ has never been far from functional programming, with features such as function pointers, functors, and the algorithms from STL allowing many programmers to take advantage of certain constructs. However, starting with C++ 11, we see the introduction of lambdas, and of higher-order functions such as all_of, any_of, and none_of. In C++ 17, we see more progress, with the introduction of map (implemented as transform). Additionally, the features coming in C++ 20 are very exciting; for example, the ranges library, which allows composable, lightweight, and lazily evaluated transformations, is a great addition to the standard.

This brings us to what you will learn from this book. Whether you are a seasoned programmer or a C++ beginner, you will learn about functional programming concepts, how to use them in C++, and why they are useful for managing and improving existing code bases. Every idea will be showcased with clear code samples and verified with unit tests; we highly encourage you to take these code samples and play around with them yourself.

Special effort has been put into ensuring that every idea is presented in a clear manner, and that a flow of understanding is followed; in other words, we've been looking at optimizing your learning experience. In order to do that, we have decided to exaggerate the use of certain constructs. For example, the sample code uses a lot of lambdas since we wanted to show how they can be used. We believe that the best way to learn functional programming is to fully dive into the world of lambdas and operations on lambdas. We expect the reader to separate this approach from a production approach; in fact, I advise you to experiment with these concepts on your own, then on small parts of production code, and only then use those that are promising to their full extent. To support this goal, we have documented multiple ways of using operations on functions so that you will possess enough tools to use in various contexts.

It's important to note that we made a calculated decision to present the C++ 17 standard in most of the book. We don't use external libraries (other than the unit testing library), and we stick to the standard features of the language and of Standard Template Library (STL). The focus is on functional programming concepts and on how to implement them using a minimalistic approach. The only exception is the last section of the book that looks at the future of C++ and STL. We did this because we believe that it's more important for you to understand the concepts and be ready to apply them with minimal tooling than to provide a multitude of implementation options. This has left out the ranges library for most of the book, the Boost library support for functional programming, and, most likely, other useful libraries that can extend or simplify the code. I will leave it to the reader to try them out for themselves and let us know how they worked.