Summary
So far, we have been acquainted with the functional approach by discussing the introduction of functional programming. We also have compared the functional approach to the mathematical concept when we create functional program. It's now clear that the functional approach uses the mathematical approach to compose a functional program.
There are three important points in constructing the function; they are definition, script, and session. The definition is the equation between particular expressions that describe the mathematical function. Script is a collection of definitions that are supplied by the programmer. Session is a situation where the program submits the expressions that can contain references to the function defined in the script to the computer for evaluation.
The comparison between functional and imperative programming also led us to the important point of distinguishing the two. It's now clear that in functional programming, the programmer focuses on the kind of desired information and the kind of required transformation, while in the imperative approach, the programmer focuses on the way of performing the task and tracking changes in the state.
We also explored several concepts of functional programming, such as first-class and higher-order functions, pure functions, and recursive functions. The first-class and higher-order functions concept treats the functions as values so that we can assign it to a variable and pass it to the argument of the function. The pure functions concept makes the function have no side-effect. Recursive functions help us iterate the function itself with the power of aggregate in LINQ. Also, functions in functional programming have several characteristics that we need to know, such as the following:
It always returns the same value every time it is given the same set of inputs.
It never references a variable defined outside the function.
It cannot change the value of the variable since it applies the immutable concept.
It doesn't contain any I/O, such as a fancy output or a keyboard stroke, since no side-effect occurrence is allowed.
When we test functional program in C#, we take the mathematical approach to find out how to compose a function in C# from a mathematical function. We learn how to curry the curried function to pass the second argument after we assign the first argument alone. Also, we now know how to make the program functional using pipelining and the method chaining technique.
After finishing with learning the techniques for creating functional programming, we translate the imperative approach code into the functional approach code. Here, we compose the imperative code from scratch and then refactor it into functional code.
Lastly, after we become more familiar with functional programming, we can grasp the advantages and disadvantages of functional programming itself. This will be the reason why we need to learn functional programming.
In the next chapter, we will talk about delegate data type to encapsulates a method that has particular parameters and return type. It is useful when we need create a cleaner and an easier function pointer.