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"),
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]
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]
filter in the Haskell Standard Prelude
filter in SRFI 1
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.