Array.tryPick<'T,'U> 関数 (F#)

指定された関数を一連の要素に適用し、関数が Some を返す最初の結果を返します。どの要素についても関数が Some を返さない場合は、None が返されます。

名前空間/モジュール パス: Microsoft.FSharp.Collections.Array

アセンブリ: FSharp.Core (FSharp.Core.dll 内)

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

// Usage:
Array.tryPick chooser array

パラメーター

  • chooser
    型: 'T -> 'U option

    配列要素をオプションに変換する関数。

  • array
    型: 'T[]

    入力配列。

戻り値

Some のオプション値を持つ最初に変換された要素、またはどの要素についても関数が Some を返さない場合は None。

解説

この関数は、コンパイルされたアセンブリでは TryPick という名前です。F# 以外の言語から、またはリフレクションを使用してこの関数にアクセスする場合は、この名前を使用します。

使用例

Array.tryPick を使用して、条件を満たす要素を見つけ、その要素に関する特定の追加データ (この場合は平方根と立方根) を返す例を次に示します。

let findPerfectSquareAndCube array1 =
    let delta = 1.0e-10
    let isPerfectSquare (x:int) =
        let y = sqrt (float x)
        abs(y - round y) < delta
    let isPerfectCube (x:int) =
        let y = System.Math.Pow(float x, 1.0/3.0)
        abs(y - round y) < delta
    // intFunction : (float -> float) -> int -> int
    // Allows the use of a floating point function with integers.
    let intFunction function1 number = int (round (function1 (float number)))
    let cubeRoot x = System.Math.Pow(x, 1.0/3.0)
    // testElement: int -> (int * int * int) option
    // Test an element to see whether it is a perfect square and a perfect
    // cube, and, if so, return the element, square root, and cube root
    // as an option value. Otherwise, return None.
    let testElement elem = 
        if isPerfectSquare elem && isPerfectCube elem then
            Some(elem, intFunction sqrt elem, intFunction cubeRoot elem)
        else None
    match Array.tryPick testElement array1 with
    | Some (n, sqrt, cuberoot) -> printfn "Found an element %d with square root %d and cube root %d." n sqrt cuberoot
    | None -> printfn "Did not find an element that is both a perfect square and a perfect cube."

findPerfectSquareAndCube [| 1 .. 10 |]
findPerfectSquareAndCube [| 2 .. 100 |]
findPerfectSquareAndCube [| 100 .. 1000 |]
findPerfectSquareAndCube [| 1000 .. 10000 |]
findPerfectSquareAndCube [| 2 .. 50 |]
  
  
  
  
  

プラットフォーム

Windows 8、Windows 7、Windows Server 2012 で Windows Server 2008 R2

バージョン情報

F# コア ライブラリのバージョン

サポート: ポータブル 2.0、4.0

参照

関連項目

Collections.Array モジュール (F#)

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