Seq.filter<'T> Function (F#)
Returns a new collection containing only the elements of the collection for which the given predicate returns true.
Namespace/Module Path: Microsoft.FSharp.Collections.Seq
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
Seq.filter : ('T -> bool) -> seq<'T> -> seq<'T>
// Usage:
Seq.filter predicate source
The returned sequence may be passed between threads safely. However, individual IEnumerator values generated from the returned sequence should not be accessed concurrently. The sequence is lazily evaluated. Therefore, effects are delayed until it is enumerated.
This function is named Filter in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.
The following code shows the use of Seq.filter to filter an infinite sequence of random numbers to select only even numbers.
let random = new System.Random() Seq.initInfinite (fun _ -> random.Next()) |> Seq.filter (fun x -> x % 2 = 0) |> Seq.take 5 |> Seq.iter (fun elem -> printf "%d " elem) printfn ""
Sample Output
2140052690 963487404 467169526 1800517368 1225141818