Functional Programming in C++

Book description

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.



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.

We interviewed Ivan as a part of our Six Questions series. Check it out here.



Quotes
Offers precise, easy-to-understand, and engaging explanations of functional concepts.
- Sumant Tambe, LinkedIn

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

Table of contents

  1. Titlepage
  2. Copyright
  3. preface
  4. acknowledgments
  5. about this book
    1. 1.1 Who should read this book
    2. Roadmap
    3. Code conventions and downloads
    4. Book forum
  6. about the author
  7. Chapter 1: Introduction to functional programming
    1. 1.1 What is functional programming?
      1. 1.1.1 Relationship with object-oriented programming
      2. 1.1.2 A concrete example of imperative vs. declarative programming
    2. 1.2 Pure functions
      1. 1.2.1 Avoiding mutable state
    3. 1.3 Thinking functionally
    4. 1.4 Benefits of functional programming
      1. 1.4.1 Code brevity and readability
      2. 1.4.2 Concurrency and synchronization
      3. 1.4.3 Continuous optimization
    5. 1.5 Evolution of C++ as a functional programming language
    6. 1.6 What you’ll learn in this book
    7. Summary
  8. Chapter 2: Getting started with functional programming
    1. 2.1 Functions taking functions?
    2. 2.2 Examples from the STL
      1. 2.2.1 Calculating averages
      2. 2.2.2 Folding
      3. 2.2.3 String trimming
      4. 2.2.4 Partitioning collections based on a predicate
      5. 2.2.5 Filtering and transforming
    3. 2.3 Composability problems of STL algorithms
    4. 2.4 Writing your own higher-order functions
      1. 2.4.1 Receiving functions as arguments
      2. 2.4.2 Implementing with loops
      3. 2.4.3 Recursion and tail-call optimization
      4. 2.4.4 Implementing using folds
    5. Summary
  9. Chapter 3: Function objects
    1. 3.1 Functions and function objects
      1. 3.1.1 Automatic return type deduction
      2. 3.1.2 Function pointers
      3. 3.1.3 Call operator overloading
      4. 3.1.4 Creating generic function objects
    2. 3.2 Lambdas and closures
      1. 3.2.1 Lambda syntax
      2. 3.2.2 Under the hood of lambdas
      3. 3.2.3 Creating arbitrary member variables in lambdas
      4. 3.2.4 Generic lambdas
    3. 3.3 Writing function objects that are even terser than lambdas
      1. 3.3.1 Operator function objects in STL
      2. 3.3.2 Operator function objects in other libraries
    4. 3.4 Wrapping function objects with std::function
    5. Summary
  10. Chapter 4: Creating new functions from the old ones
    1. 4.1 Partial function application
      1. 4.1.1 A generic way to convert binary functions into unary ones
      2. 4.1.2 Using std::bind to bind values to specific function arguments
      3. 4.1.3 Reversing the arguments of a binary function
      4. 4.1.4 Using std::bind on functions with more arguments
      5. 4.1.5 Using lambdas as an alternative for std::bind
    2. 4.2 Currying: a different way to look at functions
      1. 4.2.1 Creating curried functions the easier way
      2. 4.2.2 Using currying with database access
      3. 4.2.3 Currying and partial function application
    3. 4.3 Function composition
    4. 4.4 Function lifting, revisited
      1. 4.4.1 Reversing a list of pairs
    5. Summary
  11. Chapter 5: Purity: Avoiding mutable state
    1. 5.1 Problems with the mutable state
    2. 5.2 Pure functions and referential transparency
    3. 5.3 Programming without side effects
    4. 5.4 Mutable and immutable state in a concurrent environment
    5. 5.5 The importance of being const
      1. 5.5.1 Logical and internal const-ness
      2. 5.5.2 Optimizing member functions for temporaries
      3. 5.5.3 Pitfalls with const
    6. Summary
  12. Chapter 6: Lazy evaluation
    1. 6.1 Laziness in C++
    2. 6.2 Laziness as an optimization technique
      1. 6.2.1 Sorting collections lazily
      2. 6.2.2 Item views in the user interfaces
      3. 6.2.3 Pruning recursion trees by caching function results
      4. 6.2.4 Dynamic programming as a form of laziness
    3. 6.3 Generalized memoization
    4. 6.4 Expression templates and lazy string concatenation
      1. 6.4.1 Purity and expression templates
    5. Summary
  13. Chapter 7: Ranges
    1. 7.1 Introducing ranges
    2. 7.2 Creating read-only views over data
      1. 7.2.1 Filter function for ranges
      2. 7.2.2 Transform function for ranges
      3. 7.2.3 Lazy evaluation of range values
    3. 7.3 Mutating values through ranges
    4. 7.4 Using delimited and infinite ranges
      1. 7.4.1 Using delimited ranges to optimize handling input ranges
      2. 7.4.2 Creating infinite ranges with sentinels
    5. 7.5 Using ranges to calculate word frequencies
    6. Summary
  14. Chapter 8: Functional data structures
    1. 8.1 Immutable linked lists
      1. 8.1.1 Adding elements to and removing them from the start of a list
      2. 8.1.2 Adding elements to and removing them from the end of a list
      3. 8.1.3 Adding elements to and removing them from the middle of a list
      4. 8.1.4 Memory management
    2. 8.2 Immutable vector-like data structures
      1. 8.2.1 Element lookup in bitmapped vector tries
      2. 8.2.2 Appending elements to bitmapped vector tries
      3. 8.2.3 Updating elements in bitmapped vector tries
      4. 8.2.4 Removing elements from the end of the bitmapped vector trie
      5. 8.2.5 Other operations and the overall efficiency of bitmapped vector tries
    3. Summary
  15. Chapter 9: Algebraic data types and pattern matching
    1. 9.1 Algebraic data types
      1. 9.1.1 Sum types through inheritance
      2. 9.1.2 Sum types through unions and std::variant
      3. 9.1.3 Implementing specific states
      4. 9.1.4 Special sum type: Optional values
      5. 9.1.5 Sum types for error handling
    2. 9.2 Domain modeling with algebraic data types
      1. 9.2.1 The naive approach, and where it falls short
      2. 9.2.2 A more sophisticated approach: Top-down design
    3. 9.3 Better handling of algebraic data types with pattern matching
    4. 9.4 Powerful pattern matching with the Mach7 library
    5. Summary
  16. Chapter 10: Monads
    1. 10.1 Not your father’s functors
      1. 10.1.1 Handling optional values
    2. 10.2 Monads: More power to the functors
    3. 10.3 Basic examples
    4. 10.4 Range and monad comprehensions
    5. 10.5 Failure handling
      1. 10.5.1 std::optional<T> as a monad
      2. 10.5.2 expected<T, E> as a monad
      3. 10.5.3 The Try monad
    6. 10.6 Handling state with monads
    7. 10.7 Concurrency and the continuation monad
      1. 10.7.1 Futures as monads
      2. 10.7.2 Implementations of futures
    8. 10.8 Monad composition
    9. Summary
  17. Chapter 11: Template metaprogramming
    1. 11.1 Manipulating types at compile-time
      1. 11.1.1 Debugging deduced types
      2. 11.1.2 Pattern matching during compilation
      3. 11.1.3 Providing metainformation about types
    2. 11.2 Checking type properties at compile-time
    3. 11.3 Making curried functions
      1. 11.3.1 Calling all callables
    4. 11.4 DSL building blocks
    5. Summary
  18. Chapter 12: Functional design for concurrent systems
    1. 12.1 The actor model: Thinking in components
    2. 12.2 Creating a simple message source
    3. 12.3 Modeling reactive streams as monads
      1. 12.3.1 Creating a sink to receive messages
      2. 12.3.2 Transforming reactive streams
      3. 12.3.3 Creating a stream of given values
      4. 12.3.4 Joining a stream of streams
    4. 12.4 Filtering reactive streams
    5. 12.5 Error handling in reactive streams
    6. 12.6 Replying to the client
    7. 12.7 Creating actors with a mutable state
    8. 12.8 Writing distributed systems with actors
    9. Summary
  19. Chapter 13: Testing and debugging
    1. 13.1 Is the program that compiles correct?
    2. 13.2 Unit testing and pure functions
    3. 13.3 Automatically generating tests
      1. 13.3.1 Generating test cases
      2. 13.3.2 Property-based testing
      3. 13.3.3 Comparative testing
    4. 13.4 Testing monad-based concurrent systems
    5. Summary
  20. Index

Product information

  • Title: Functional Programming in C++
  • Author(s): Ivan Cukic
  • Release date: January 2019
  • Publisher(s): Manning Publications
  • ISBN: 9781617293818