Compartilhar 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 -> 'Uoption

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

Reference

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)