Functor Design Pattern
Lesson Introduction
Welcome! Today, we're diving into an essential concept in functional programming: the Functor Design Pattern. You might ask, "What is a functor, and why should I care?"
As you might remember we talked about functors in the previous units. In C++ a "functor" usually refers to a class implementing the () operator, which can be called like a function. However, in more general terms, a functor is a different concept.
Generally speaking, Functors allow you to map a function over a structure, making your code clean and modular. By the end of this lesson, you'll know how to create and use functors in C++ to transform data structures effectively. Let’s get started!
Understanding Functors
First, let’s clarify what a functor is. In Functional Programming, a functor refers to an object, such as a class or a template class, that provides a method (transform or map) to apply a function to some specific element.
In more general terms, consider a template class F. This template class is considered a functor if it defines a map (or transform) method. This method should take two arguments:
- A value of type
F<T1>. - A function
tthat transforms a value of typeT1to a value of typeT2.
The map (transform) method then applies the function to the value inside the structure F and returns a new structure F<T2> with the transformed value.
Let's see an example to understand this concept better.
Creating the Functor Class
First, let’s define the Functor class and understand each line of code step-by-step.
template<typename T>: This specifies thatFunctoris a template class parameterized byT, which represents the type of the value inside thestd::optional.template<typename U>: This specifies that thetransformmethod is itself a template method, allowing different types for the input (T) and output (U) elements.std::optional<U> transform(const std::optional<T>& value, std::function<U(T)> func): This is the declaration of thetransformmethod.- It takes two parameters:
const std::optional<T>& value: A constant reference to an optional holding an element of typeT.std::function<U(T)> func: A function object taking an input of typeTand returning an output of typeU.
- It returns an optional containing a transformed element if the input optional has a value, or
std::nulloptif the input optional is empty.
- It takes two parameters:
