Functional JavaScript First Steps

filter, map, & reduce Solution: filter

Anjana Vakil

Anjana Vakil

Software Engineer & Educator
Functional JavaScript First Steps

Check out a free preview of the full Functional JavaScript First Steps course

The "filter, map, & reduce Solution: filter" Lesson is part of the full, Functional JavaScript First Steps course featured in this preview video. Here's what you'd learn in this lesson:

Anjana demonstrates the solution to the filter, map & reduce exercise. This segment covers implementing predicate functions for the filter function.


Transcript from the "filter, map, & reduce Solution: filter" Lesson

>> In the interest of time, let's gonna through together and talk about some possible solutions. Again, there might be more than one way to solve each of these. So your mileage may vary, but let's go through and see some solutions. Okay, so for this filter function, we thought we have [COUGH] this kind of recursive implementation of this filter function where we're successively filtering things in an array.

And we take in that predicate function, where that predicate function again, it's gonna be something that takes in a single value and returns a boolean. So we've got some of these predicate functions like isEven. You might have solved this different ways. But one way you could solve this is by using the modulo operator in JavaScript, so to see whether or not there's something leftover when you divide this number by two and if not, then it's even.

So that's one implementation that will get these little to show up. And how about the odds function? Well, if odd is kind of the opposite of even then we could pass in our predicate function that basically returns the opposite of whatever is even returns, so that's one way we could do this.

We could also maybe have implemented this more directly like this but with a not equal. Or there might have been other solutions, but these are two that you could have used. And then for the greaterThanFour function we could have used and you might wanna do a short little arrow function here.

Which is handy for working in functional programming in JavaScript because you don't have to keep typing that function keyword all the time. So arrows, they came in with ES6 syntax, are super handy for doing functional programming. And if you haven't worked with those a lot, I encourage you to use these examples to kind of get a sense for how that syntax feels.

So we might use the greater than operator to take in a number and then return the boolean for whether or not that number is greater than four. Any questions so far? Great. Okay, so then a little bit more complicated one is this isPrime function. Now there are lots of different ways you could have done this.

Here's one way, so again cheating a little bit because I know kind of what the types of values are that we're gonna get here. I took in the this this array with all these whole numbers and I declared it inside of this function, so that we don't have to look anywhere in the outside world for this.

And now you might be wondering in a real life isPrime, I probably wanna be able to figure this out for things that go above 10. But in some functional programming languages, you might be able to work with things like lazy evaluated sequence, that's a term to type into the search engine later.

You might be able to work with sequences that can give you an array like this of whatever length you need. Because thanks to functional programming and this concept of only returning an element from a sequence, when it's actually asked for. We could get something that would work, that would be a version of this implementation that could work for even even higher numbers.

This one will be pretty limited in what it's gonna be accurate for. But in any case, so given some kind of sequence of like whole numbers, what I'm doing and again, this is just how I solved it, yours might be totally different. What I'm doing is I'm figuring out some possible factors.

Where basically the number that's coming in, we have to find some kind of number that's between one and that number n, that might be a factor of n. So what I'm doing is kind of filtering down this wholes array so that we're getting things that are between one and whatever my end number is.

And then taking those possible factors, I'm determining which of them actually are factors. By using this filter function, which, similar to our isEven function uses modulo to find out whether n is perfectly divisible by m, and then and oopsie. I'm using a method here instead of my function, that's better, that's more functional.

Then I'm just checking to see if there's anything in this factors array. If there is then that means that this number is divisible by more than just itself and one which by the definition of a prime number means it's not prime. So essentially we only have a prime number if that factors array is zero.

So this i thought was an interesting function to implement as a predicate function because we can actually use filter inside of a predicate function that then we can pass to filter. So this is again, this is when we start getting used to working with arrays, excuse me working with these tools like filter map and reduce, and working with functions that we can pass around to other functions.

You may find a lot of the time this is almost like a type of kind of multi level filtering that's happening, where we're using filter to actually help us define functions that we can pass into a filter. So it's sort of like an aura boris, a filter in here.

Again, your implementation may have looked different, but as long as you're using pure functions. And as long as you're trying to avoid those array method, object oriented style method APIs and use these helper functions that we defined, you should be good to go

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