Check out a free preview of the full Functional-Light JavaScript, v2 course:
The "Data Structure Operations Introduction" Lesson is part of the full, Functional-Light JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle reviews lists, which are typically visualized as arrays, but can be generalized as any data structure that represents/produces an ordered collection of values. As such, all these "list operations" are actually "data structure operations."

Get Unlimited Access Now

Transcript from the "Data Structure Operations Introduction" Lesson

[00:00:00]
>> Kyle: Okay, our next discussion point in the overall discussion about list operations, is I wanted to take you back to something I said at the beginning of this part of our discussion. Which was that really these are actually operations that are generalized any data structure, not just the operations that would only work on arrays.

[00:00:20] So I wanna give you an example of that. This is a map object utility, which does the same thing as map for a list would do, but it does it across the list of enumerable keys in an object. Some important things to point out, I create a brand new object on line 2 called newObj, and then on line 3 I get the list of enumerable properties and a list of enumerable keys for that object, with the object.keys utility.

[00:00:50] Line 4, I loop over those keys and I'm using the four of loop. Don't miss that detail. So that I actually am looking at the keys rather than the indexes of the keys array. And then, I set the value and the new object at that same property location, equal to the mapped transformed value of the original object at that location.

[00:01:17] That's should look exactly familiar. The same concept is what we did with the array. It's just slightly different, so that it's been adapted to work against an object instead of against an array.
>> Kyle: Moreover, any data structure that you could think of, you could implement these same fundamental ideas of map, filter and reduce, for any data structure you provide.

[00:01:41] And moreover not only can you, I'm gonna strongly implore you that you should. Any data structure that is custom that you've built that isn't an array, that you're tracking data, your data model come up with some kind of adapter that adapts the concepts of map, filter and reduce to it.

[00:01:59] Because that will make working with that object and doing operations against it, not just easier, but more functional. I think one of the key underlying concepts here, is that functional programmers do work with data structures. They do work with types, and the way they do it is to adapt functional concepts to those data structures.

[00:02:22] And map object here is an example of adapting functional programming, to a key value hash. You can see how it's used down there at the bottom, I call map obj. I pass in my mapper function, and my O and I get back a new object where each of those values was uppercased.

[00:02:48]
>> Kyle: To get you some practice with thinking this way, abstracting your list operations thought process to data structures, and here's a hint, to thinking about your own code as a data structure. That is, if I have a list of operations in my code, I could turn that list of operations into a list operation.

[00:03:14] I could turn that list of statements into a list operation. That kind of thing actually happens a lot more often than I initially realized, and it's really where I think functional programming and list operations start to shine, is when our program itself becomes a data structure that we can model these ideas across.

[00:03:34] And actually, we've already seen a couple of examples of doing that. Do you remember when a little while ago in the fusion discussion, I said well what if I have a composed write and I can only do two of them? Well, you could have done that imperatively. You could have called compose right with the first two of them, and then called compose right again with that one plus another one.

[00:03:58] You could have done that manually. But what did I do? Instead of doing two separate statements like that and storing the intermediary result, what did I do? I modeled those statements as a list operation themselves. By putting the functions into an array and reducing across the array. And that allowed me to make those statements as a list operation, instead of individual statements.

[00:04:23] In a sense, I treated that part of my program as a data structure, to operate against. That is where functional programming for me is really starting to get more fascinating, it's really starting to shine.