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.
true
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
even
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).
.
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 : f ( x ) = { T r u e if x ≡ 0 ( mod 2 ) F a l s e if x ≡ 1 ( mod 2 ) . {\displaystyle f(x)={\begin{cases}\mathrm {True} &{\text{ if }}x\equiv 0{\pmod {2}}\\\mathrm {False} &{\text{ if }}x\equiv 1{\pmod {2}}.\end{cases}}}
X = [0, 5, 8, 3, 2, 1]
This function express that if x {\displaystyle x} is even the return value is T r u e {\displaystyle \mathrm {True} } , otherwise it's F a l s e {\displaystyle \mathrm {False} } . This is the predicate.
Filter is a standard function for many programming languages, e.g., Haskell,[1] OCaml,[2] Standard ML,[3] or Erlang.[4] Common Lisp provides the functions remove-if and remove-if-not.[5] Scheme Requests for Implementation (SRFI) 1 provides an implementation of filter for the language Scheme.[6] C++ provides the algorithms remove_if (mutating) and remove_copy_if (non-mutating); C++11 additionally provides copy_if (non-mutating).[7] Smalltalk provides the select: method for collections. Filter can also be realized using list comprehensions in languages that support them.
remove-if
remove-if-not
remove_if
remove_copy_if
copy_if
select:
In Haskell, filter can be implemented like this:
filter
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).
[]
++
[x | p x]
x
p x
True
(pred array)/array
pred{⍵/⍨⍺⍺ ⍵}array
{⍵/⍨⍺⍺ ⍵}
ienum.Where(pred)
where
obj.filter(func)
obj
func
(filter predicate list)[8]
(for [x list :when (pred x)] x)
(remove-if inverted-pred list)(remove-if (complement pred) list)(remove-if-not pred list)
(remove-if-not #'oddp '(0 1 2 3))
(remove-if (complement #'oddp) '(0 1 2 3))
(remove-if #'evenp '(0 1 2 3))
evenp
oddp
std::remove_copy_if(begin, end, result, prednot)std::copy_if(begin, end, result, pred) (C++11)
std.algorithm.filter!(pred)(list)
lists:filter(Fun, List)
[ X || X <- List, Fun(X) ]
list.findAll(pred)
filter pred list
[x | x <- list, pred x]
list.filter(pred)
Lambda.filter(list, pred)
(#~ pred) list
(f g) y = y f (g y)
filter(pred, array)
dict
[x for x in array if pred(x)]
stream.filter(pred)
array.filter(pred)
Select[list, pred]
[array filteredArrayUsingPredicate:pred]
pred
List.filter pred list
select(expr, list)
grep block list grep expr, list
array_filter(array, pred)
filter(+Closure,+List,-List)
call/N
filter(func, list)
[x for x in list if pred(x)]
filterfalse
itertools
enum.find_all {block} enum.select {block}
enum
iterator.filter(pred)
iterator
Iterator
FnMut
bool
Filter(pred,array)array[pred(array)]
for(x <- list; if pred) yield x
(filter pred list)
(remove inverted pred list)
(partition pred list list)
aCollection select: aBlock
array.filter(pred) filter(sequence, pred)
list[block]
filter(list, func)
block
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[14] and partition[15]) 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).
dropWhile
partition
list
filter/2
lists