Functional Programming in C++
How to improve your C++ programs using functional techniques
Ivan Čukić
  • MEAP began November 2016
  • Publication in October 2018 (estimated)
  • ISBN 9781617293818
  • 325 pages (estimated)
  • printed in black & white

A book all functional programming aficionados have been waiting for! Reading "Functional Programming in C++" is an eye-opening experience.

Dimitris Papadopoulos

Functional programming is becoming a required skill for all programmers, and for good reason. The functional style of programming lets you write more concise code which tends to have fewer bugs. It allows you to decompose your programs into smaller, highly reusable components, without putting conceptual limits on how the software should be modularized. FP makes software development easier and software more correct and robust. These techniques can be used as a complement to those from other programming paradigms, for example OOP, which makes FP a great fit for C++ as the most popular multi-paradigm language.

Table of Contents detailed table of contents

1 Introduction to functional programming

1.1 What is functional programming?

1.1.1 Relationship with object-oriented programming

1.1.2 A concrete example of the difference between the imperative and declarative styles of programming

1.2 Pure functions

1.2.1 Avoiding mutable state

1.3 Thinking functionally

1.4 Benefits of functional programming

1.4.1 Code brevity and readability

1.4.2 Concurrency and synchronization

1.4.3 Continuous optimization

1.5 Evolution of C++ as a functional programming language

1.6 What you will learn in this book

1.7 Summary

2 Getting started with functional programming

2.1 Functions taking functions?

2.2 Examples from the STL, algorithm library

2.2.1 Calculating averages

2.2.2 Folding

2.2.3 String trimming

2.2.4 Partitioning collections based on a predicate

2.2.5 Filtering and transformation

2.3 Composability problems of STL algorithms

2.4 Writing your own higher order functions

2.4.1 Receiving functions as arguments

2.4.2 Implementing with loops

2.4.3 Recursion and tail call optimization

2.4.4 Implementing using folds

2.5 Summary

3 Function objects

3.1 Functions and function objects

3.1.1 Automatic return type deduction

3.1.2 Function pointers

3.1.3 Call operator overloading

3.1.4 Creating generic function objects

3.2 Lambdas and closures

3.2.1 Lambda syntax

3.2.2 Under the hood of lambdas

3.2.3 Creating arbitrary member variables in lambdas

3.2.4 Generic lambdas

3.3 Writing function objects that are even terser than lambdas

3.3.1 Operator function objects in STL

3.3.2 Operator function objects in other libraries

3.4 Wrapping function objects with std::function

3.5 Summary

4 Creating new functions from the old ones

4.1 Partial function application

4.1.1 A generic way of converting binary functions into unary ones

4.1.2 Using std::bind to bind values to specific function arguments

4.1.3 Reversing the arguments of a binary function

4.1.4 Using std::bind on functions with more arguments

4.1.5 Using lambdas as an alternative for std::bind

4.2 Currying — a different way to look at functions

4.2.1 Creating curried functions the easier way

4.2.2 Using currying with database access

4.2.3 Currying and partial function application

4.3 Function composition

4.4 Function lifting revisited

4.4.1 Reversing a list of pairs

4.5 Summary

5 Purity - avoiding mutable state

5.1 Problems with the mutable state

5.2 Pure functions and referential transparency

5.3 Programming without side-effects

5.3.1 Creating new worlds instead of changing the current one

5.4 Mutable and immutable state in a concurrent environment

5.5 The importance of being const

5.5.1 Logical and internal constness

5.5.2 Optimizing member functions for temporaries

5.5.3 Pitfalls with const

5.6 Summary

6 Lazy evaluation

6.1 Laziness in C++

6.2 Laziness as an optimization technique

6.2.1 Sorting collections lazily

6.2.2 Item views in the user interfaces

6.2.3 Recursion tree pruning by caching the function results

6.2.4 Dynamic programming as a form of laziness

6.3 Generalized memoization

6.4 Expression templates and lazy string concatenation

6.4.1 Purity and expression templates

6.5 Summary

7 Ranges

7.1 Introduction to ranges

7.2 Creating read-only views over data

7.2.1 Filter function for ranges

7.2.2 Transform function for ranges

7.2.3 Lazy evaluation of range values

7.3 Mutating values through ranges

7.4 Delimited and infinite ranges

7.4.1 Delimited ranges optimize handling input ranges

7.4.2 Sentinels also give us infinite ranges

7.5 Using ranges to calculate word frequencies

7.6 Summary

8 Functional data structures

8.1 Immutable linked lists

8.1.1 Adding and removing elements to/from the start of the list

8.1.2 Adding and removing elements to/from the end of the list

8.1.3 Adding and removing elements from/to the middle of the list

8.1.4 Memory management

8.2 Immutable vector-like data structure

8.2.1 Element lookup in bitmapped vector tries

8.2.2 Appending elements to bitmapped vector tries

8.2.3 Updating elements in bitmapped vector tries

8.2.4 Removing elements from the end of the bitmapped vector trie

8.2.5 Other operations and the overall efficiency of bitmapped vector tries

8.3 Summary

9 Algebraic data types and pattern matching

9.1 Algebraic data types

9.1.1 Sum types through inheritance

9.1.2 Sum types through unions and std::variant

9.1.3 Implementing specific states

9.1.4 Special sum type — optional values

9.1.5 Sum types for error handling

9.2 Domain modelling with the algebraic data types

9.2.1 The naive approach, and where it falls short

9.2.2 A more sophisticated approach: top-down design

9.3 Better handling of algebraic data types with pattern matching

9.4 Powerful pattern matching with the Mach7 library

9.5 Summary

10 Monads

10.1 Not your father’s functors

10.1.1 Handling optional values

10.2 Monads — more power to the functors

10.3 Basic examples

10.4 Range and monad comprehensions

10.5 Failure handling

10.5.1 std::optional<T> as a monad

10.5.2 expected<T, E> as a monad

10.5.3 Try monad

10.6 Handling state with monads

10.7 Concurrency and the continuation monad

10.7.1 Futures as monads

10.7.2 Implementations of the future

10.8 Monad composition

10.9 Summary

11 Template meta-programming

11.1 Manipulating types at compile-time

11.1.1 Debugging deduced types

11.1.2 Pattern matching during compilation

11.1.3 Providing meta-information about types

11.2 Checking type properties at compile-time

11.3 Making curried functions

11.3.1 Calling all callables

11.4 DSL building blocks

11.5 Summary

12 Functional design for concurrent systems

12.1 The actor model — thinking in components

12.2 Creating a simple message source

12.3 Modelling reactive streams as monads

12.3.1 Creating a sink to receive messages

12.3.2 Transforming reactive streams

12.3.3 Creating a stream of given values

12.3.4 Joining a stream of streams

12.4 Filtering reactive streams

12.5 Error handling in reactive streams

12.6 Replying to the client

12.7 Creating actors with a mutable state

12.8 Writing distributed systems with actors

12.9 Summary

13 Testing and debugging

13.1 Is the program that compiles correct?

13.2 Unit testing and pure functions

13.3 Automatically generating tests

13.3.1 Generating test cases

13.3.2 Property-based testing

13.3.3 Comparative testing

13.4 Testing monad-based concurrent systems

13.5 Summary


About the book

Functional Programming in C++ teaches developers the practical side of functional programming and what tools C++ provides to develop software in the functional style. This practical guide starts with an introduction to functional programming that contrasts FP with the imperative style of programming. This book is full of helpful diagrams that help you to understand FP concepts and begin to think functionally. You'll learn, and with the help of lots of code examples, try out FP concepts like monads, lazy evaluation, and immutable data structures. The last few chapters take you deeper into real-world FP, covering functional design for concurrent and distributed systems as well as testing and debugging.

What's inside

  • Using functional parts of the standard library
  • Implementing algorithms without incurring performance penalties
  • Creating high-level abstractions for specific domains
  • Designing composable component-based systems
  • Thinking about program state in a functional way
  • Explicitly handling errors through the type system
  • Understanding important features in the next C++ standard
  • Extending C++ with new control structures
  • Implementing DSLs to compose concurrent tasks

About the reader

This book is for developers who have worked professionally with C++ for 2 or more years.

About the author

Ivan Čukić has been teaching modern C++ techniques and functional programming at the Faculty of Mathematics in Belgrade for 6 years and has been using C++ for more than 15 years. He has been researching functional programming in C++ before and during his PhD studies, and uses the techniques in real-world projects. He is one of the core developers in KDE, the largest free/libre open source C++ project.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $49.99 pBook + eBook + liveBook
MEAP eBook $39.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks

This is an excellent intermediate C++ book, suitable for people who have not had (or only had minimal) exposure to functional programming and thinking.

Marco Massenzio