共用方式為


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

清單的每個項目要套用特定的函式,並傳回結果,每個項目所組成的清單,函式會傳回 Some與 某些值。

命名空間/模組路徑:Microsoft.FSharp.Collections.Seq

組件:FSharp.Core (在 FSharp.Core.dll 中)

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

// Usage:
Seq.choose chooser source

參數

  • chooser
    Type: 'T -> 'U option

    函式,將型別 T 的項目轉換成型別 U 的選項。

  • source
    型別:seq<'T>

    型別 T 的輸入序列。

例外狀況

例外狀況

條件

ArgumentNullException

當輸入序列為 null 時擲回。

傳回值

結果序列。

備註

傳回的序列可以在執行緒之間安全地傳送。 但是,個別 IEnumerator傳回序列所產生的值應該不存取並行

這個函式是名為 Choose中 已編譯的組件。 如果從一個語言,F # 以外,或透過反映存取函式使用這個名稱。

範例

下列程式碼範例示範如何使用 Seq.choose以選取項目從序列使用 Lambda 運算式的使用模式比對來傳回 選項類型。

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
  

平台

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

版本資訊

F# 執行階段

支援版本:2.0、4.0

Silverlight

支援版本:3

請參閱

參考

Collections.Seq 模組 (F#)

Microsoft.FSharp.Collections 命名空間 (F#)