Filter (higher-order function)
{{Short description|Computer programming function}}
In functional programming, filter is a higher-order function that processes a data structure (usually a list) 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 [1..10]
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 (not . even) [1..10]
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 (with .
being the function composition operator).
= Visual example =
Below, you can see a view of each step of the filter process for a list of integers X = [0, 5, 8, 3, 2, 1]
according to the function :
\mathrm{True} &\text{ if } x \equiv 0 \pmod{2}\\
\mathrm{False} & \text{ if } x \equiv 1 \pmod{2}.
\end{cases}
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.,
OCaml,[http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html filter
] in the OCaml standard library module list
Standard ML,{{cite web|url=http://www.standardml.org/Basis/list.html#SIG:LIST.filter:VAL|work=The Standard ML Basis Library|title=The List structure|accessdate=2007-09-25}}
or Erlang.[http://www.erlang.org/doc/doc-5.5.4/lib/stdlib-1.14.4/doc/html/lists.html#filter/2 filter/2
] in the Erlang STDLIB Reference Manual documentation of the module lists
Common Lisp provides the functions remove-if
and remove-if-not
.[http://www.lispworks.com/documentation/HyperSpec/Body/f_rm_rm.htm#remove-if-not Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, DELETE, DELETE-IF, DELETE-IF-NOT] in the Common Lisp HyperSpec
Scheme Requests for Implementation (SRFI) 1 provides an implementation of filter for the language Scheme.[http://srfi.schemers.org/srfi-1/srfi-1.html#FilteringPartitioning filter
] in SRFI 1
C++ provides the algorithms remove_if
(mutating) and remove_copy_if
(non-mutating); C++11 additionally provides copy_if
(non-mutating).[http://www.sgi.com/tech/stl/remove_if.html remove_if
] and [http://www.sgi.com/tech/stl/remove_copy_if.html remove_copy_if
] in the SGI Standard Template Library (STL) spec 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 :: (a -> Bool) -> [a] -> [a]
filter _ [] = []
filter p (x:xs) = [x | p x] ++ filter p xs
Here,
denotes the empty list, ++
the list concatenation operation, and [x | p x]
denotes a list conditionally holding a value, x
, if the condition p x
holds (evaluates to True
).
class="wikitable" style="font-size: 85%"
|+ Filter in various languages ! scope="col" | Language ! scope="col" | Filter ! scope="col" | Notes | |
APL
| | The second example is an APL [https://aplwiki.com/wiki/Dop dop]. | |
C# 3.0
| | Where is an extension method | |
CFML
| | Where | |
Clojure
| | Or, via list comprehension: | |
Common Lisp
| | The function | |
C++
| | in header | |
D
| | | |
Erlang
| | Or, via list comprehension: | X <- List, Fun(X) ] |
Groovy
| | | |
Haskell
| | Or, via list comprehension: | |
Haxe
| | Or, via list comprehension: | |
J
| | An example of a monadic hook. # is copy, ~ reverses arguments. | |
Julia
| | The filter function also accepts | |
Java 8+
| | | |
JavaScript 1.6
| | | |
Kotlin
| | | |
Mathematica
| | | |
Objective-C (Cocoa in Mac OS X 10.4+)
| | | |
F#, OCaml, Standard ML
| | | |
PARI/GP
| | The order of arguments is reversed in v. 2.4.2. | |
Perl
| | | |
PHP
| | | |
Prolog
| | Since ISO/IEC 13211-1:1995/Cor.2:2012[http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=58033 ISO/IEC 13211-1:1995/Cor 2:2012] the core standard contains closure application via | |
Python
| | Or, via list comprehension: | |
Ruby
| | | |
Rust
| | | |
S, R
| | In the second case, pred must be a vectorized function | |
Scala
| | Or, via for-comprehension: | |
Scheme R6RS
| | | |
Smalltalk
| | | |
Swift
| | | |
XPath, XQuery
| | In |
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 (e.g., Haskell dropWhile
[http://haskell.org/onlinereport/standard-prelude.html#$vdropWhile Haskell filter dropWhile] and partition
[http://www.haskell.org/onlinereport/list.html#sect17.3 Haskell filter partition]) 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 (tail-sharing).