The filter Function
Learn the higher-order function abstraction filter for filtering out elements of a list based on a given predicate.
We'll cover the following...
We'll cover the following...
Another crucial function abstraction is filter. We can use it to filter out the elements of a list based on a given condition.
The filter function on lists
Let’s write a function evens that returns only even numbers from an integer list.
We write another function, positives, that returns only positive numbers from a list of integers.
Obviously, evens and positives share a common pattern. The real difference is the function that checks the head of the list and decides whether it belongs to the result or not. We can factor out the common pattern as a higher-order function called filter, which accepts a function making a filtering ...