Table of contents
Collapse the table of content
Expand the table of content

Seq.filter<'T> Function (F#)

Den Delimarsky|Last Updated: 8/18/2017
1 Contributor

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


predicate Type: 'T ->bool

A function to test whether each item in the input sequence should be included in the output.

source Type: seq<'T>

The input sequence.


ArgumentNullExceptionThrown when the input sequence is null

Return Value

The result sequence.


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 ""


2140052690 963487404 467169526 1800517368 1225141818


Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2020 Microsoft