Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Filter: Inclusion" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle discusses the filter method, mentioning that filter is an inclusion operation, and not exclusion. Filter is recommended for when there is a collection where values are being kept in or left out of the result.

Preview
Close

Transcript from the "Filter: Inclusion" Lesson

[00:00:00]
>> Kyle Simpson: Our next operation that we wanna look at, our next list or data structure operation is called filter. Now filter is often referred to as, or thought of as an exclusionary activity. So let time explain it or illustrate it in this way. How many of you have ever made spaghetti?

[00:00:17]
Anybody? In my house, the only meal that I cook in my house is spaghetti. I can cook steaks and other meat on the griller, but the only meal that I make is spaghetti. My wife does all the other cooking and she's great at it. But I like to make spaghetti, and I enjoy the process for some reason.

[00:00:35]
And so in spaghetti, generally you have a big pot of boiling water with your spaghetti noodles in it. And at some point when the noodles are cooked, you have to pour out the water and keep the noodles, right? So what do you do, you take a strainer, you take a filter.

[00:00:50]
You hold it over the sink, you pour everything in. And the water goes through, but the noodles are caught in the strainer, okay? Now, if I were to describe that process and ask you, would you be more naturally, or would you be more normally inclined to describe that as filtering out the water, or filtering in the noodles?

[00:01:14]
>> Kyle Simpson: And I don't know what your particular answer is, but most of the time, I hear people say, I would call it a filter out. I'm excluding the water, and the noodles are what's left over, right? I'm not including the noodles in my set, right? So we generally think of filtering as an exclusionary activity.

[00:01:31]
And in fact, almost every place that you can think of metaphorically, or in the English language when we use the word filter, we think about it in terms of filtering something out. Which is exactly why as programmers, we decided to actually implement Filter as an inclusion operation, just to mess with people.

[00:01:50]
Just to make it reverse so that everybody gets confused. Because in Filter, as it applies to programming, it's actually a filter in. And by that I mean when you tell it whether or not you wanna keep something You return True if you want to keep it, and you return False if you don't want to keep it.

[00:02:11]
So the true semantics sort of implies that I'm filtering it in. If I went to the grocery store and there was big old pile of apples and bananas they were all mixed together, and all I wanted was the apples. And I had to sift through every single fruit pulling out all the apples into my basket and leaving all the bananas.

[00:02:27]
Would you describe that as filtering in the apples or filtering out the bananas? Most people would say, I'm filtering out the bananas, I only want the apples. If I had a list of numbers; 1, 2, 3, 4, 5, and I ended up with a list of numbers 1, 3, 5, that is the odd numbers.

[00:02:45]
Would you say that I filtered in the odds or did I filter out the evens? The problem is that we use this filter word when in English we mean filter out, but in implementation, we mean filter in. And I don't know if any of you all have ever had this, but I've had a bunch of bugs in my programs over the years.

[00:03:06]
What I was thinking filter out and it was actually filter in, and my, essentially my Boolean logic was worse. So, I have taken to my own practice, I never use the method named filter ever again. I don;t use filter, I either call it filter in or filter out.

[00:03:22]
Because I don't want to be any confusion in the program. What am I doing at this point? Am I filtering in things or am I filtering out things? I really wish we could just abolish the word filter because there's this baggage here. So we're gonna build a filter in, because it's gonna work exactly like the filter that's on the array prototype.

[00:03:40]
A filter in method, which takes a function, remember up there I called that a predicate? Remember earlier in the course, rather that I called it a predicate when a function returns a Boolean. If it returns a true false based upon some value input it's called a predicate. So the way the filter works is it loops through the list of values and it calls the predicate.

[00:04:01]
And if the predicate returns true, it includes it in the new list otherwise it just ignores it and then we end up with a new list. So what's an example of us filtering? Well if I had a list of session objects, and I wanted to figure out if any of those, I mean sorry, a list of user objects and figure out which of those users are logged in.

[00:04:22]
Meaning which of those have a session ID attached to them. Then I can use a filter in, and the filter in is logged in the function the predicate up at the top It returns True if there is a session property that's non on the object. So now I'm gonna end up with the list of users that have a session object attached versus the ones that don't have a session object attached.

[00:04:45]
>> Kyle Simpson: That's filtering in. And we can do the exact same thing with the built-in filter method on the array prototype. It does the same thing, it takes a predicate function, it returns a new array with the values included for which the predicate returns True.
>> Kyle Simpson: So, the pattern matching for how to decide is filter the operation that I want to do, is when I have some collection of value, some data structure like an array or an object.

[00:05:13]
And I want to produce the same kind of data structure, but I want some of the values to be included and potentially some of the values to be excluded.
>> Kyle Simpson: Whether you think about that as a filter in or filter out is up to you. Hopefully your method names are unambiguous so you're not gonna create bugs in your code.

[00:05:32]
But the idea is I want to start with one collection of values and end up with another collection of values. The values don't get changed at all, that's what map does. So we're not picking new values here. We're just selecting values based upon some predicate check.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now