concept filter in category functional programming

This is an excerpt from Manning's book Grokking Functional Programming.
4.31 Returning parts of the list using
filterYou can probably guess that the new requirement can be quickly implemented using a function from Scala List. You are right! This function is called filter. It’s very similar to the sortBy and map functions we have already met, because it also takes just one parameter and this parameter happens to be a function!
Let’s look at the implementation of the new requirement using Scala and its built-in immutable List that has a filter function:
def highScoringWords(wordScore: String => Int, words: List[String]): List[String] = { words.filter(word => wordScore(word) > 1) #A }
What filter does is it takes each element of the list and applies the provided condition to it, returning only elements that satisfied this condition. Again, it doesn’t mutate anything. Scala List API supports immutability, so the returned list is a fresh list of elements that satisfied the condition we provided as a function to the filter function.

This is an excerpt from Manning's book Functional Programming in C++.
The
filterconstruct takes a collection and a predicate function (T → bool) as arguments, and it returns a collection of filtered items, so its type is written like this:
Listing 2.15 Calling the helper function
template <typename FilterFunction, typename Iterator> std::vector<std::string> names_for( Iterator people_begin, Iterator people_end, FilterFunction filter) { return names_for_helper(people_begin, people_end, filter, {}); }
Figure 7.1 The view created by
filterstores an iterator to the source collection. The iterator points to only the elements that satisfy the filtering predicate. The user of this view will be able to use it as if it were a normal collection of people with only three elements in it: Martha, Jane, and Rose.![]()

This is an excerpt from Manning's book Functional Programming in JavaScript.
Compared with the previous example, you see that this code frees you from the responsibility of properly managing a loop counter and array index access; put simply, the more code you have, the more places there are for bugs to occur. Also, standard loops aren’t reusable artifacts unless they’re abstracted with functions. And that’s precisely what we’ll do. In chapter 3, I demonstrate how to remove manual loops completely from your code in favor of first-class, higher-order functions like map, reduce, and filter, which accept functions as parameters so that your code is more reusable, extensible, and declarative. This is what I did with the magical run function in listings 1.1 and 1.2.
In this chapter, I’ll introduce you to a few useful and practical operations like map, reduce, and filter that allow you to traverse and transform data structures in a sequential manner. These operations are so important that virtually all functional programs use them in one way or another. They also facilitate removing manual loops from your code, because most loops are just specific cases handled by these functions.
When processing large collections of data, it’s often necessary to remove elements that don’t form part of your computations. For instance, say you want to count only people living in European countries, or people born in a certain year. Instead of cluttering your code with if-else statements, you can use _.filter.
filter (also known as select) is a higher-order function that iterates through an array of elements and returns a new array that’s a subset of the original with values for which a predicate function p returns a result of true. In formal notation, this looks like the following (also see figure 3.6):
filter(p, [d0, d1, d2, d3...dn]) -> [d0,d1,...dn] (subset of original input)Figure 3.6. The filter operation takes an array as input and applies a selection criteria p that potentially yields a much smaller subset of the original array. The criteria p is also known as a function predicate.
![]()
Listing 3.5. filter implementation
function filter(arr, predicate) { let idx = -1, len = arr.length, result = []; #1 while (++idx < len) { let value = arr[idx]; if (predicate(value, idx, this)) { #2 result.push(value); } } return result; }In addition to the array, filter accepts a predicate function used to test each member of the array for inclusiveness. If the predicate yields true, the element is kept in the result; otherwise, the element is skipped. This is why filter is commonly used to remove invalid data from an array:
_(persons).filter(isValid).map(fullname);But it can do much more than that. Suppose you need to extract only people born in 1903 from a collection of Person objects. Applying _.filter is much easier and cleaner than using conditional statements:
