O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning C++ Functional Programming

Book Description

Apply Functional Programming techniques to C++ to build highly modular, testable, and reusable code

About This Book
  • Modularize your applications and make them highly reusable and testable
  • Get familiar with complex concepts such as metaprogramming, concurrency, and immutability
  • A highly practical guide to building functional code in C++ filled with lots of examples and real-world use cases
Who This Book Is For

This book is for C++ developers comfortable with OOP who are interested in learning how to apply the functional paradigm to create robust and testable apps.

What You Will Learn
  • Get to know the difference between imperative and functional approaches
  • See the use of first-class functions and pure functions in a functional style
  • Discover various techniques to apply immutable state to avoid side effects
  • Design a recursive algorithm effectively
  • Create faster programs using lazy evaluation
  • Structure code using design patterns to make the design process easier
  • Use concurrency techniques to develop responsive software
  • Learn how to use the C++ Standard Template Library and metaprogramming in a functional way to improve code optimization
In Detail

Functional programming allows developers to divide programs into smaller, reusable components that ease the creation, testing, and maintenance of software as a whole. Combined with the power of C++, you can develop robust and scalable applications that fulfill modern day software requirements. This book will help you discover all the C++ 17 features that can be applied to build software in a functional way.

The book is divided into three modules—the first introduces the fundamentals of functional programming and how it is supported by modern C++. The second module explains how to efficiently implement C++ features such as pure functions and immutable states to build robust applications. The last module describes how to achieve concurrency and apply design patterns to enhance your application’s performance. Here, you will also learn to optimize code using metaprogramming in a functional way.

By the end of the book, you will be familiar with the functional approach of programming and will be able to use these techniques on a daily basis.

Style and approach

This book uses a module-based approach, where each module will cover important aspects of functional programming in C++ and will help you develop efficient and robust applications through gaining a practical understanding.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Diving into Modern C++
    1. Getting closer with several new features in modern C++
      1. Defining the data type automatically using the auto keyword
      2. Querying the type of an expression using the decltype keyword
      3. Pointing to a null pointer
      4. Returning an iterator using non-member begin() and end() function
      5. Iterating over collections using range-based for loops
    2. Leveraging the use of C++ language with the C++ Standard Libraries
      1. Placing any objects in the container
      2. Using algorithms
    3. Simplifying the function notation using a Lambda expression
      1. Using the Lambda expression for a tiny function
      2. Using the Lambda expression for multiline functions
      3. Returning a value from the Lambda expression
      4. Capturing a value to the Lambda expression
      5. Preparing the value using initialization captures
      6. Writing a generic Lambda expression to be used many times with many different data types
    4. Avoiding manual memory management with smart pointers
      1. Replacing a raw pointer using unique_ptr
      2. Sharing objects using shared_ptr
      3. Tracking the objects using a weak_ptr pointer
    5. Storing many different data types using tuples
      1. Unpacking tuples values
      2. Returning a tuple value type
    6. Summary
  3. Manipulating Functions in Functional Programming
    1. Applying the first-class function in all functions
      1. Passing a function as another function's parameter
      2. Assigning a function to a variable
      3. Storing a function in the container
      4. Creating a new function from the existing functions at runtime
    2. Getting acquainted with three functional techniques in the higher-order function
      1. Executing each element list using map
      2. Extracting data using filter
      3. Combining all elements of a list using fold
    3. Avoiding the side effect with pure function
    4. Reducing a multiple arguments function with currying
    5. Summary
  4. Applying Immutable State to the Function
    1. Understanding the essential part from immutable object
      1. Modifying a local variable
      2. Modifying a variable passed into a function
    2. Preventing the modification of a value
    3. Applying the first-class function and the pure function to the immutable object
    4. Developing the immutable object
      1. Starting with a mutable object
      2. Refactoring a mutable object into an immutable one
    5. Enumerating the benefits of being immutable
    6. Summary
  5. Repeating Method Invocation Using Recursive Algorithm
    1. Repeating the function invocation recursively
      1. Performing the iteration procedure to repeat the process
      2. Performing the recursion procedure to repeat the process
    2. Recurring the immutable function
    3. Getting closer to tail recursion
    4. Getting acquainted with functional, procedural, and backtracking recursion
      1. Expecting results from functional recursion
      2. Running a task recursively in procedural recursion
      3. Backtracking recursion
    5. Summary
  6. Procrastinating the Execution Process Using Lazy Evaluation
    1. Evaluating the expression
      1. Running the expression immediately with strict evaluation
      2. Delaying the expression with non-strict evaluation
    2. The basic concept of lazy evaluation
      1. Delaying the process
      2. Caching the value using the memoization technique
      3. Optimizing the code using the memoization technique
    3. Lazy evaluation in action
      1. Designing Chunk and Row classes
      2. Concatenating several rows
      3. Iterating each Row class' element
      4. Generating the infinite integer row
      5. Generating an infinite prime numbers row
      6. Refactoring eager evaluation to lazy evaluation
    4. Summary
  7. Optimizing Code with Metaprogramming
    1. Introduction to metaprogramming
      1. Preprocessing the code using a macro
      2. Dissecting template metaprogramming in the Standard Library
    2. Building the template metaprogramming
      1. Adding a value to the variable in the template
      2. Mapping a function to the input parameters
      3. Choosing the correct process based on the condition
      4. Repeating the process recursively
    3. Selecting a type in compile-time
    4. Flow control with template metaprogramming
      1. Deciding the next process by the current condition
      2. Selecting the correct statement
      3. Looping the process
    5. Executing the code in compile-time
      1. Getting a compile-time constant
      2. Generating the class using a compile-time class generation
    6. Benefits and drawbacks of metaprogramming
    7. Summary
  8. Running Parallel Execution Using Concurrency
    1. Concurrency in C++
      1. Processing a single threading code
      2. Processing a multithreading code
    2. Synchronizing the threads using mutex
      1. Avoiding synchronization issues
      2. Unlocking the variable automatically
      3. Avoiding deadlock using recursive mutex
    3. Understanding the thread processing in a Windows operating system
      1. Working with handle
      2. Refactoring to a unique handle
      3. Triggering an event
      4. Calling an event from a thread
    4. Summary
  9. Creating and Debugging Application in Functional Approach
    1. Preparing an imperative class
    2. Refactoring the imperative class to become a functional class
      1. Passing a function as a parameter
      2. Adding a base class
      3. Transforming the class to become pure
      4. Filtering the condition and implementing a Lambda expression
      5. Implementing recursion and memoization techniques to the Customer class
    3. Debugging the code
      1. Starting the debugging tool
      2. Continuing and stepping the debugging process
      3. Setting and deleting the breakpoint
      4. Printing the object value
    4. Summary