Share via


Array.tryFind<'T> 関数 (F#)

指定した関数が true を返す最初の要素を返します。そのような要素が存在しない場合は、None を返します。

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

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

// Signature:
Array.tryFind : ('T -> bool) -> 'T [] -> 'T option

// Usage:
Array.tryFind predicate array

パラメーター

  • predicate
    型: 'T ->bool

    入力要素をテストする関数。

  • array
    型: 'T[]

    入力配列。

戻り値

述語を満たす最初の要素または None。

解説

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

使用例

Array.tryFind を使用して、完全立方かつ完全平方である配列要素を検索するコード例を次に示します。

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
let lookForCubeAndSquare array1 =
    let result = Array.tryFind (fun elem -> isPerfectSquare elem && isPerfectCube elem) array1
    match result with
    | Some x -> printfn "Found an element: %d" x
    | None -> printfn "Failed to find a matching element."

lookForCubeAndSquare [| 1 .. 10 |]
lookForCubeAndSquare [| 100 .. 1000 |]
lookForCubeAndSquare [| 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#)