Filter (higher-order function)


In functional programming, filter is a higher-order function that processes a data structure in some order to produce a new data structure containing exactly those elements of the original data structure for which a given predicate returns the boolean value true.

Example

In Haskell, the code example

filter even

evaluates to the list 2, 4,…10 by applying the predicate even to every element of the list of integers 1, 2,… 10 in that order and creating a new list of those elements for which the predicate returns the boolean value true, thereby giving a list containing only the even members of that list. Conversely, the code example

filter

evaluates to the list 1, 3,…9 by collecting those elements of the list of integers 1, 2… 10 for which the predicate even returns the boolean value false.

Visual example

Below, you can see a view of each step of the filter process for a list of integers X = according to the function :
This function express that if is even the return value is , otherwise it's . This is the predicate.

Language comparison

Filter is a standard function for many programming languages, e.g.,
Haskell,
OCaml,
Standard ML,
or Erlang.
Common Lisp provides the functions remove-if and remove-if-not.
Scheme Requests for Implementation 1 provides an implementation of filter for the language Scheme.
C++ provides the algorithms remove_if and remove_copy_if ; C++11 additionally provides copy_if. Smalltalk provides the select: method for collections. Filter can also be realized using list comprehensions in languages that support them.
In Haskell, filter can be implemented like this:

filter :: -> ->
filter _ =
filter p = ++ filter p xs

Here, denotes the empty list, ++ the list concatenation operation, and denotes a list conditionally holding a value, x, if the condition p x holds.
LanguageFilterNotes-
APL/array-
C# 3.0ienum.Where
or
Where is an extension method
ienum is an IEnumerable
Similarly in all.NET languages
-
CFMLobj.filterWhere obj is an array or a structure. The func receives as an argument each element's value.-
ClojureOr, via list comprehension: -
Common Lisp

The function remove-if-not has been deprecated in favor of the equivalent remove-if where the predicate is complemented. Thus the filter should be written ') or more simply: where evenp returns the inverted value of oddp.
C++std::remove_copy_if
std::copy_if
in header
begin, end, result are iterators
predicate is reversed
-
Dstd.algorithm.filter!--
Erlanglists:filterOr, via list comprehension: -
Groovylist.findAll-
Haskellfilter pred listOr, via list comprehension: -
Haxelist.filter
Lambda.filter
Or, via list comprehension: -
J listAn example of a monadic hook. # is copy, ~ reverses arguments. y = y f -
JuliafilterThe filter function also accepts dict datatype. Or, via list comprehension: -
Java 8+stream.filter-
JavaScript 1.6array.filter-
Kotlinarray.filter-
MathematicaSelect-
Objective-C pred is an object, which may be limited in expressiveness-
F#, OCaml, Standard MLList.filter pred list-
PARI/GPselectThe order of arguments is reversed in v. 2.4.2.-
Perlgrep block list
grep expr, list
-
PHParray_filter-
PrologfilterSince ISO/IEC 13211-1:1995/Cor.2:2012 the core standard contains closure application via call/N-
PythonfilterOr, via list comprehension: . In Python 3.x, filter was changed to return an iterator rather than a list. The complementary functionality, returning an iterator over elements for which the predicate is false, is also available in the standard library as filterfalse in the itertools module.-
Rubyenum.find_all
enum.select
enum is an Enumeration-
S, RFilter
array
In the second case, pred must be a vectorized function-
Scalalist.filterOr, via for-comprehension: for yield x-
Scheme R6RS

-
SmalltalkaCollection select: aBlock-
Swiftarray.filter
filter
-
XPath, XQuerylist
filter
In block the context item . holds the current value-

Variants

Filter creates its result without modifying the original list. Many programming languages also provide variants that destructively modify the list argument instead for faster performance. Other variants of filter are also common. A common memory optimization for purely functional programming languages is to have the input list and filtered result share the longest common tail.