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

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.

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.

Table of Contents detailed table of contents

1. Introduction to functional programming

1.1. What is functional programming?

1.1.1. Relation to the 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 cxx as a functional programming language

1.6. 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. Functional objects

3.1. Functions and functional 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 in C++11

3.2.2. Under the hood of lambdas

3.2.3. Lambdas in C++14

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 functional 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

8. Data structures

9. Pattern matching

10. Monads

11. Template meta-programming

12. Functional design for concurrent and distributed systems

13. 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
MEAP eBook $39.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks