true
.
Example
Ineven
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
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 integersX = , 5, 8, 3, 2, 1/code> 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 language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
s, e.g.,
Haskell,filter
in the Haskell Standard Prelude
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language
Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms.
...
,filter
in the OCaml
OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language
Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms.
...
standard library module list
Standard ML
Standard ML (SML) is a general-purpose, modular, functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of ...
,
or Erlang.
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fr ...
provides the functions remove-if
and 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 Scheme Requests for Implementation (SRFI) is an effort to coordinate libraries and extensions of standard Scheme programming language, necessitated by Scheme's minimalist design, and particularly the lack of a standard library before Scheme R6RS. SR ...
(SRFI) 1 provides an implementation of filter for the language Scheme A scheme is a systematic plan for the implementation of a certain idea.
Scheme or schemer may refer to:
Arts and entertainment
* ''The Scheme'' (TV series), a BBC Scotland documentary series
* The Scheme (band), an English pop band
* ''The Schem ...
.filter
in SRFI 1
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
provides the algorithms
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
remove_if
(mutating) and remove_copy_if
(non-mutating); C++11 C11, C.XI, C-11 or C.11 may refer to:
Transport
* C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War
* Fokker C.XI, a 1935 Dutch reconnaissance seaplane
* LET C-11, a license-build ...
additionally provides copy_if
(non-mutating).remove_if
an
in the SGI Standard Template Library
The Standard Template Library (STL) is a software library originally designed by Alexander Stepanov for the C++ programming language that influenced many parts of the C++ Standard Library. It provides four components called ''algorithms'', '' ...
(STL) spec Smalltalk
Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan ...
provides the select:
method for collections. Filter can also be realized using list comprehension
A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical ''set-builder notation'' (''set comprehension'') as distinct from the use of ...
s in languages that support them.
In Haskell, filter
can be implemented like this:
filter :: (a -> Bool) -> -> 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
).
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
and partition
Haskell filter ''partition''
/ref>) are also common. A common memory optimization for purely functional programming language
In computer science, purely functional programming usually designates a programming paradigm—a style of building the structure and elements of computer programs—that treats all computation as the evaluation of mathematical functions.
Progra ...
s is to have the input list and filtered result share the longest common tail ( tail-sharing).
See also
* Map (higher-order function)
* List comprehension
A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical ''set-builder notation'' (''set comprehension'') as distinct from the use of ...
* Guard (computing)
References
{{Reflist
Higher-order functions
Articles with example Haskell code