We discuss what functional programming is, some of the main benefits of functional programming, and introduce a few very important concepts for implementing functional concepts in your codebase
- [Instructor] So what is functional programming exactly? Well, at its core functional programming is just another programming paradigm. Similar to other paradigms such as imperative programming, object-oriented programming, and logic programming, it represents a way of organizing the large number of complex ideas present in a computer program in a coherent way. It's also worth noting that, like each of these other paradigms, there is a certain range of problems which functional programming solves especially well. One of the major goals of functional programming is optimal arrangement of a code base.
In other words, as a code base gets larger and more complex, it gets more and more necessarily to arrange it in such a way that changes and additions can be made quickly, easily, and without introducing new bugs in the process. Of course, this is also one of the main goals of object-oriented programming, which it achieves with varied success. Since many of you are probably most familiar with object-oriented programming, I'll use it in a lot of comparisons with functional programming to clarify the concepts I show you. Functional programming is declarative as opposed to imperative.
In simpler terms, this means that functional programming puts more focus on what things are as opposed to how to get them. For example, let's say that we want a program that tells us the average of an array of numbers. Imperative programming would specify the steps required to calculate the result; something like set X equal to zero; add the first number in the array to X; repeat step two for the rest of the numbers in the array; and then divide X by the length of the array. Unless you've worked with functional programming before, this is probably how you're used to writing programs, thinking of how to get the result.
On the other hand, if we wanted to solve the same problem in a declarative way, it would be more like simply stating X is the sum of all the numbers in the array, divided by the length of the array. Now, if it never occurred to you that it's possible to write programs in this way by simply stating what something is, you're not alone. This way of talking about things may also remind some of you of mathematical functions such as these. And as a matter of fact, this is one of the main ideas behind functional programming, to bring the precision of mathematical functions into our programs.
As you may have figured, this is also where functional programming gets its name. Now, it's all very easy to say that functional programming brings the precision of mathematics into programming, but what does that mean exactly? Well, there are three main concepts behind functional programming which allow us to do just that. These concepts are: separating functions and data, immutability, and first-class functions. Over the course of the next few videos, I'll go into depth on what each of these concepts means, contrast it with how other paradigms do things, and explain its advantages.
- What is functional programming?
- Keeping functions and data separate
- First-class functions
- Working with arrays functionally
- Filtering and reducing
- Partial-application and recursion