共用方式為


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

將指定的函式套用至陣列的每一個元素。 傳回結果 x函式會傳回其中每個項目為 所組成的陣列 Some(x)

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

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

// Signature:
Array.choose : ('T -> 'U option) -> 'T [] -> 'U []

// Usage:
Array.choose chooser array

參數

  • chooser
    Type: 'T -> 'U option

    用來從項目產生選項的函式。

  • array
    Type: 'T []

    輸入陣列。

傳回值

結果的陣列。

備註

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

範例

下列程式碼示範如何使用 Array.choose執行作業只能在偶數的 陣列。

printfn "%A" (Array.choose (fun elem -> if elem % 2 = 0 then
                                            Some(float (elem*elem - 1))
                                        else
                                            None) [| 1 .. 10 |])
  

平台

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.Array 模組 (F#)

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