From the course: Learning Functional Programming with JavaScript ES6+

Basics of functional data manipulation

- [Instructor] All right, so now that we've taken a look at first class functions, which as you've seen, give you a lot of power and flexibility in your programs, the next thing that we're going to take a look at here is the basics of manipulating data in a functional way, right? So in other words, when you have some data and you want to modify it, how do you do that in functional programming? Well, let's start off by taking a look at the procedural way of making these kinds of changes. In the procedural way, we generally write our programs in such a way that we're specifying how to calculate the result, right? So in other words, we're specifying a process or procedure, hence procedural, for how to calculate the result that we're looking for. And this generally involves a series of steps. First, we're going to initialize a variable, then we're going to loop through all of the elements in an array, and this is usually done using a for loop, and generally that for loop will modify the variable that we initialized, so that once we're done with the loop, that variable is going to contain the result. Now, the problem with this is that between the step where we initialize the variable and when we're done and have the result, that variable is kind of in this uncomfortable intermediate point where it's not really the result, but it's also not what we initialized it as. And this can make our programs pretty difficult to troubleshoot if something doesn't work the way we want it to, as I'm sure you've experienced when working with things like complex or nested loops. So, the functional way of manipulating data, I really think you're going to like this way a lot better. The first thing to know about the functional way of manipulating data is that instead of specifying how to calculate the result and the steps required to calculate the result, we simply write our programs in such a way that it tells JavaScript what it is that we want. That might sound a little bit strange, you'll see exactly what this looks like very shortly. But generally it means that we don't use any for loops, right? So instead of for loops, we're generally going to use a number of other functions with names like map, filter, reduce, and sort. And these functions will allow us to get the exact same result and apply the exact same operations as we were able to do with a for loop, but in a much more functional and declarative way. Now, we'll get into these functions shortly, but one thing that I want you to remember here before we get into these functions is that with some of these functions, you might find yourself wondering how the function is actually working, right? What process is actually going on behind the scenes with a lot of these functions? For example, with the sort function, you might wonder, is the function using quick sort, merge sort? You know, what kind of algorithm is it using behind the scenes? Well, I'm going to recommend that you do your best to not worry about how these things actually work behind the scenes, right? That's not really the point. The point of these functions is to allow you to express what you want instead of how to calculate what you want in a declarative way. So again, just keep that in mind. We'll see what this looks like in much more detail when we jump into these functions, which we're going to do right now.

Contents