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.
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 :
[4] Common Lisp provides the functions remove-if and remove-if-not.
Filter can also be realized using list comprehensions in languages that support them.
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).