Share via


Seq.choose<'T,'U> Function (F#)

Applies the given function to each element of the list and returns the list comprised of the results for each element where the function returns Some with some value.

Namespace/Module Path: Microsoft.FSharp.Collections.Seq

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
Seq.choose : ('T -> 'U option) -> seq<'T> -> seq<'U>

// Usage:
Seq.choose chooser source

Parameters

  • chooser
    Type: 'T -> 'U option

    A function to transform items of type T into options of type U.

  • source
    Type: seq<'T>

    The input sequence of type T.

Exceptions

Exception

Condition

ArgumentNullException

Thrown when the input sequence is null.

Return Value

The result sequence.

Remarks

The returned sequence may be passed between threads safely. However, individual IEnumerator values generated from the returned sequence should not be accessed concurrently.

This function is named Choose in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following code example demonstrates the use of Seq.choose to select elements from a sequence by using a lambda expression that uses pattern matching to return an option type.

let numbers = seq {1..20}
let evens = Seq.choose(fun x -> 
                            match x with
                            | x when x%2=0 -> Some(x)
                            | _ -> None ) numbers
printfn "numbers = %A\n" numbers
printfn "evens = %A" evens
numbers = seq [1; 2; 3; 4; ...]

evens = seq [2; 4; 6; 8; ...]

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)