Functional Programming in C++
How to improve your C++ programs using functional techniques
Ivan Čukić
  • November 2018
  • ISBN 9781617293818
  • 320 pages
  • printed in black & white
ePub + Kindle available Dec 10, 2018

Offers precise, easy-to-understand, and engaging explanations of functional concepts.

Sumant Tambe, LinkedIn

Functional Programming in C++ teaches developers the practical side of functional programming and the tools that C++ provides to develop software in the functional style. This in-depth guide is full of useful diagrams that help you understand FP concepts and begin to think functionally.

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

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

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

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

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 const-ness

5.5.2 Optimizing member functions for temporaries

5.5.3 Pitfalls with const

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

Summary

7 Ranges

7.1 Introduction 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

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

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

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

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

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

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

Summary

About the Technology

Well-written code is easier to test and reuse, simpler to parallelize, and less error prone. Mastering the functional style of programming can help you tackle the demands of modern apps and will lead to simpler expression of complex program logic, graceful error handling, and elegant concurrency. C++ supports FP with templates, lambdas, and other core language features, along with many parts of the STL.

About the book

Functional Programming in C++ helps you unleash the functional side of your brain, as you gain a powerful new perspective on C++ coding. You’ll discover dozens of examples, diagrams, and illustrations that break down the functional concepts you can apply in C++, including lazy evaluation, function objects and invokables, algebraic data types, and more. As you read, you’ll match FP techniques with practical scenarios where they offer the most benefit.

What's inside

  • Writing safer code with no performance penalties
  • Explicitly handling errors through the type system
  • Extending C++ with new control structures
  • Composing tasks with DSLs

About the reader

Written for developers with two or more years of experience coding in C++.

About the author

Ivan Čukić is a core developer at KDE and has been coding in C++ since 1998. He teaches modern C++ and functional programming at the Faculty of Mathematics at the University of Belgrade.


placing your order...

Don't refresh or navigate away from the page.

FREE domestic shipping on three or more pBooks

An excellent read. Comprehensive code examples illustrate the implementation of functional programming patterns using C++14/C++17 constructs.

Keerthi Shetty, FactSet Research Systems

Provides elegant, easy-to-grasp, ready-to-use examples that will improve the way you think about coding.

Nikos Athanasiou, BETA CAE Systems

Presents a new way of writing quality software and a new way of thinking.

Gian Lorenzo, Meocci, CommProve

Particularly valuable for intermediate/advanced C++ developers who want to embrace reactive-style programming.

Marco Massenzio, Apple