Book Review: Functional Programming in C++

Written by on December 2, 2018 in Book Review, C++, Programming with 2 Comments

Title: Functional Programming in C++: How to improve your C++ programs using functional techniques

Author: Ivan Cukic

Publisher: Manning Publications Co.

Year: 2019

This book came out recently, about a week ago. I bought it immediately, and when I started to read it, I couldn’t put it down! Very informative and well written.

If you are new to C++, it is better to read the book after gaining some familiarity with C++ first. You might also benefit by going through other books on Functional Programming (FP) as supplementary reading material.

Before I delve into the book, I want to highlight the fact that C++ is NOT a functional programming language, just as it is NOT an object-oriented programming language. It is a multi-paradigm programming language, and allows us to choose the appropriate mix of language features for solving a given problem. The exciting news is that more exotic features are expected in C++20 (and beyond). So, as C++ programmers, we are truly privileged!

Here is an overview of what the book covers.

This book is a sincere attempt to lay a solid foundation for how C++ could be used as a functional language.

The first two chapters provide the necessary introductory material for what comes later, by giving an overview of the principles of functional programming in general, and the features of C++ that enable this. 

The third chapter goes into the details of function objects in C++, including normal functions, classes that behave like function objects, std::function<>, lambdas, and even gives some examples from the Boost.Phoenix library. For people familiar with C++, this chapter might appear somewhat redundant, but I feel the author has done the right thing by including it because it provides continuity and makes the book more widely accessible.

In the fourth chapter, the author explains the notion of partial functions and how to implement them in C++ using std::bind<> and lambda functions. Then he describes what currying is and how curried functions can be implemented using nested lambda functions. Many people find the difference between partial functions and currying difficult to understand, but this chapter provides a good overview of the topics.  There is also a brief discussion on function composition and function lifting.

The fifth chapter is about pure functions and how to write code that does not have side effects (not a trivial task in many cases). The sixth chapter is about lazy evaluation and memoization, common themes in functional programming.

The seventh chapter discusses Ranges, a feature that is planned for C++20. The author explains the idea using the range-v3 library (somewhat similar to Boost.Range). I merely skimmed through this chapter, deciding to wait for official support of the feature in the language.

In the next chapter, there is a nice explanation of functional data structures. These are data structures that are immutable and hence support efficient copying. Bartosz Milewski, another experienced C++ developer, has implemented a few functional data structures in C++ and here is a video talk given by him.

The next two chapters talk about Algebraic Data Types and Monads. std::optional<> is an example of Monad, as well as the forthcoming std::expected<>. If you would like an overview of Monads, I recommend this video tutorial. You might also want to watch this video on std::expected<> planned for C++20.

Chapter 11 covers Template Metaprogramming. This is not directly concerned with functional programming, but the idea is useful in implementing FP abstractions. I liked the last section of this chapter, which touches upon DSLs.

Chapter 12 details the complexity of building concurrent systems and gives FP-based solutions for the same. This is easily one of the best chapters in the book.

The last chapter focusses on Testing and Debugging. In addition to showing how to write test cases, the author ably demonstrates how it is possible to even generate test cases automatically in certain cases!

Functional programming is gaining a lot of attention these days. Although one could use a pure functional language such as Haskell, being able to apply functional programming techniques in a mainstream language such as C++ has immense benefits. Doing this correctly in C++ might take a while for novice C++ developers (perhaps even for experienced ones!), but the effort is well worth it.

Ivan Cukic, a respected software developer and researcher, clearly demonstrates his knowledge and deep understanding of the subject through this book. Here is a brief video talk on the subject by him.

Buy the book. Read it. Read it again. If you are a C++ programmer, you will definitely become a better C++ programmer. In fact, you will become a better programmer, period!

Have a great weekend!

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

2 Reader Comments

Trackback URL Comments RSS Feed

  1. Ashok T says:

    A comprehensive review of the book. Hope I can lay my hands on it.

  2. David Bakin says:

    Yes, very good book. The description of monads, building up from primitives transform and join to get to mbind is really quite good, I think it would make monads very clear to someone new to the concept.

Leave a Reply

Your email address will not be published. Required fields are marked *

Top