filter
function is another higher-order function for working with lists. As the name suggests, it is used for filtering lists by selecting only elements that satisfy a predicate defined by the function passed in. Like map
, we could also define filter using a list comprehension:map
, the filter
function must receive a function that returns a boolean as its argument, and the resulting list from filter
will always be of the same type as the list we passed in as we are only selecting elements from that list, rather than applying the function on them. We can filter
a list using our squareGt100
function from before in order to get the elements for which squareGt100
returns True
:filter
with other pre-defined functions: