1. When is functional programming used?
Let's talk a bit about when to use functional programming. We'll dive into some specific applications and examples, and then go over the advantages and disadvantages of this paradigm. Let's go!
2. Applications of functional programming
So what are some common modern applications of functional programming? Many of them are relevant to the fields of data science, analytics, and engineering! Functional programming is used in certain advanced data science applications including machine learning, deep learning, and other artificial intelligence tasks.
Functional programming can also be used for large-scale data processing tasks that are repeatable and for which it's very important to get consistent results for the same input every time. Finally, data engineers are increasingly making use of functional programming for their tasks, and two popular languages that employ a functional paradigm are Scala and Clojure.
3. Example of functional programming
Here is a high-level example of how a pure function might be used as part of a large-scale data processing application. We have defined a function called process_data that takes some input data stored in a parameter called raw_data. The function then first creates a new variable to store the results of the processing, called processed_data. Next, this function will go through some cleaning and processing steps before finally returning the newly processed data. You can see that no external data sources or variables are accessed, and there are no "side effects", so this is a pure function.
4. Pros and cons of functional programming
Now that we've seen some of the benefits of pure functions as well as their limitations, and the common applications for functional programming, let's shed some light on the specific advantages and disadvantages of this paradigm.
The main advantages of functional programming are that pure functions are easier to read and debug as well as being easier to test given their lack of side effects. The result is that functional programming code will have fewer unintended consequences. Pure functions are also more likely to be reusable from one project to the next, and can sometimes be run in parallel to make code execute more quickly.
In terms of disadvantages, functional programming can be tricky to get accustomed to and can feel limiting, especially since most of what we typically want to do with a program (like writing to files) would be considered a "side effect". Different programming languages take different approaches to address this. Further, functional programming is not as commonplace as some other paradigms, and so fewer experts, tools, and frameworks supporting it exist. This also leads to fewer educational resources and a steeper learning curve in general. Finally, functional programs tend to use a lot of memory, which can limit its applications.
5. Functional programming and declarative programming
One final point to cover before we move onto exercises is that functional programming is a type of declarative programming. Recall that declarative programming refers to a category of paradigms in which the computer is instructed on broadly what to do rather than the specific steps on how to do it.
Functional programming is simply one type of declarative programming, and there are others. But we consider functional programs to be declarative because the programmer only instructs the computer on which functions to execute, and does not specify the exact flow of the program, as in imperative programming paradigms.
6. Let's practice!
Great! Now we've reached the end of this video on when to use functional programming. It's time to put some of this knowledge to the test with some exercises.