List.findIndex<'T> 関数 (F#)

指定した述語を満たすリスト内の最初の要素のインデックスを返します。そのような要素が存在しない場合は、KeyNotFoundException を発生させます。

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

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

// Signature:
List.findIndex : ('T -> bool) -> 'T list -> int

// Usage:
List.findIndex predicate list

パラメーター

  • predicate
    型: 'T ->bool

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

  • list
    型: 'Tlist

    入力リスト。

例外

例外

状態

ArgumentException

リストのすべての要素について述語が false に評価された場合にスローされます。

戻り値

述語を満たす最初の要素のインデックス。

解説

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

使用例

次のコードでは、List.findIndex を使用する方法を示し、その動作を List.find と比較しています。

let list1 = [ 2 .. 100 ]
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 element = List.find (fun elem -> isPerfectSquare elem && isPerfectCube elem) list1
let index = List.findIndex (fun elem -> isPerfectSquare elem && isPerfectCube elem) list1
printfn "The first element that is both a square and a cube is %d and its index is %d." element index

出力

  

プラットフォーム

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

バージョン情報

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

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

参照

関連項目

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

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