Filter (functional programming)

A filter, in functional programming, is usually used to test each element of a list. A built-in variable, also known as a predicate (often typed out as "pred"), may represent each element being tested. If the boolean test result was true, those elements become part of a new list.

Depending on the language, the code can be arranged differently. ; in Haskellfilter in the Haskell Standard Prelude and Scheme.filter in SRFI 1 ; in Julia, Python, PHP, and R. ; in JavaScript, Kotlin, and V (Vlang).

 Examples 

In the below,  and  are predicates. Filter is used to create a new array.

array = [1, 2, 3, 4, 5, 6]
filter (even) array -- [2, 4, 6]

array := [1, 2, 3, 4, 5, 6]
new_array := array.filter(it % 2 == 0) // [2, 4, 6]

 References 

Category:Computer science
Category:Computer terminology
Category:Computer programming