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 examplefilter 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 examplefilter
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 integersX =
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.Language | Filter | Notes | - |
APL | /array | - | |
C# 3.0 | ienum.Where or | Where is an extension method ienum is an IEnumerable Similarly in all.NET languages | - |
CFML | obj.filter | Where obj is an array or a structure. The func receives as an argument each element's value. | - |
Clojure |
| Or, 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 | in header begin, end, result are iterators predicate is reversed | - |
D | std.algorithm.filter! | - | - |
Erlang | lists:filter | Or, via list comprehension:
| - |
Groovy | list.findAll | - | |
Haskell | filter pred list | Or, via list comprehension:
| - |
Haxe | list.filter Lambda.filter | Or, via list comprehension:
| - |
J | list | An example of a monadic hook. # is copy, ~ reverses arguments. y = y f | - |
Julia | filter | The filter function also accepts dict datatype. Or, via list comprehension:
| - |
Java 8+ | stream.filter | - | |
JavaScript 1.6 | array.filter | - | |
Kotlin | array.filter | - | |
Mathematica | Select | - | |
Objective-C |
| pred is an object, which may be limited in expressiveness | - |
F#, OCaml, Standard ML | List.filter pred list | - | |
PARI/GP | select | The order of arguments is reversed in v. 2.4.2. | - |
Perl | grep block list | - | |
PHP | array_filter | - | |
Prolog | filter | Since ISO/IEC 13211-1:1995/Cor.2:2012 the core standard contains closure application via call/N | - |
Python | filter | Or, 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. | - |
Ruby | enum.find_all | enum is an Enumeration | - |
S, R | Filter | In the second case, pred must be a vectorized function | - |
Scala | list.filter | Or, via for-comprehension: for yield x | - |
Scheme R6RS |
| - | |
Smalltalk | aCollection select: aBlock | - | |
Swift | array.filter | - | |
XPath, XQuery | list filter | In block the context item . holds the current value | - |