- Filter (higher-order function)
In
functional programming , filter is ahigher-order function that processes adata structure (typically 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 theboolean 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 predicateeven
returns the boolean value false (with.
being thefunction composition operator ).Implementation
Filter is a standard function for many
programming language s, e.g.Haskell, [http://haskell.org/onlinereport/standard-prelude.html#$vfilterfilter
] in the Haskell Standard Prelude]Objective Caml , [http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.htmlfilter
] in theObjective Caml standard library modulelist
]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/2filter/2
] in the Erlang STDLIB Reference Manual documentation of the modulelists
]Common Lisp provides the functionsremove-if
andremove-if-not
. [http://www.lisp.org/HyperSpec/Body/fun_removecm__elete-if-not.html#remove-if-notremove-if-not
] in theCommon Lisp HyperSpec ]
SRFI 1 provides an implementation of filter for theScheme programming language . [http://srfi.schemers.org/srfi-1/srfi-1.html#FilteringPartitioningfilter
] in SRFI 1]C++ provides the algorithmsremove_if
(mutating) andremove_copy_if
(non-mutating). [http://www.sgi.com/tech/stl/remove_if.htmlremove_if
] and [http://www.sgi.com/tech/stl/remove_copy_if.htmlremove_copy_if
] in the SGI STL spec]Smalltalk provides theselect:
method for collections. Filter can also be realized usinglist comprehension s in languages that support them.In Haskell,
filter
can be implemented like this: filter :: (a -> Bool) -> [a] -> [a] filter _ [] = [] filter p (x:xs) | p x = x : filter p xs
otherwise = filter p xsHere,
denotes the empty list, and[] :
denotes the concatenation operator used to create a new list from a given value and an existing list.Variants
Filter creates its result without modifying the original list. Many programming languages also provide variants that destructively modify the list argument instead for performance reasons. Other variants of filter (like e.g. [http://haskell.org/onlinereport/standard-prelude.html#$vdropWhile
dropWhile
] and [http://www.haskell.org/onlinereport/list.html#sect17.3partition
] ) are also common. A commonmemory optimization forpurely functional programming languages is to have the input list and filtered result share the longest common tail (tail-sharing ).References
See also
*
Map (higher-order function)
*List comprehension
*Guard (computing)
Wikimedia Foundation. 2010.