Filter (functional programming)
higher-order function that selects elements from a data structure based on a predicate function From Wikipedia, the free encyclopedia
Remove ads
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. filter pred list
; in Haskell[1] and Scheme[2]. filter(pred, array)
; in Julia, Python,[3] PHP, and R. array.filter(pred)
; in JavaScript, Kotlin, and V (Vlang).[4]
Remove ads
Examples
In the below, even
and it
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
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads