Transcript from the "Boolean Operations" Lesson
>> Richard Feldman: All right, [COUGH] so let's put booleans to some use here. So let's talk about booleans for a list membership. So let's say we've got this list, 1, 2, and 3. And I wanna call List.member on it, passing the number 1 as the thing that I wanna check the membership of.
[00:00:17] So List.member 1 in the list [1, 2, 3] says is 1 a member of this list? And this function's going to return true because, yes, indeed 1 is, in fact, a member of the list 1, 2, and 3, it's in there somewhere. You could also say List.member 9 in this list, and that's gonna return false because the number 9 is not a member of this list, it's nowhere in there.
[00:00:40] We're gonna use this a little bit in the exercise in conjunction with filtering lists. So, again let's say we have a list [1, 2, 3], and we want strip it down to a smaller list that has fewer values in it by looking at each of the values and deciding if it's keepable, if we want to keep that value.
[00:01:02] So to do this, we'll write a little function called is keepable, which is gonna accept one of these numbers in the list. And it's gonna return either true or false to specify whether or not we wanna keep it after we perform our filter. So let's say we specify our isKeepable function to be, keep it only if the number is greater than 1.
[00:01:20] So we call List.filter, passing this isKeepable function and then the list 1, 2, and 3. And what filters gonna do is it's gonna iterate over this list and each one is gonna call isKeepable on it. So it's gonna say, is 1 greater than 1? Not it's not, so isKeepable returns false.
[00:01:36] Which means we are not going to keep that, we're gonna drop it out of the list. Next one, 2, is 2 greater than 1? Yeah, that is keepable, true. Is that keepable? Also True. So the end result of this is gonna be, it's gonna return a new list 2 and 3.
[00:01:49] It's gonna have dropped 1 because 1 was not keepable according to the function that we passed. And we can pass whatever function we want here to filter the list in whatever ways that we want. Okay, one way we could do it is this way with a named function.
[00:02:03] We could also just write an anonymous function, like we saw earlier. Just remember that backslash syntax with the arrow. So saying, \num -> num > 1, same thing as naming the function isKeepable. That would allow us to do it in line like this. And again, we'll get the same answer.
[00:02:22] So anonymous functions are commonly used with filter like this, just the same way that they are in map. In general, when you tend to be iterating over things in Elm, anonymous functions are a pretty common tool for the job of doing that.