Partager via


Array.findIndex<'T>, fonction (F#)

Retourne l'index du premier élément du tableau qui répond au prédicat donné. Si aucun des éléments ne répond au prédicat, lève KeyNotFoundException.

Espace de noms/Chemin du module : Microsoft.FSharp.Collections.Array

Assembly : FSharp.Core (in FSharp.Core.dll)

// Signature:
Array.findIndex : ('T -> bool) -> 'T [] -> int

// Usage:
Array.findIndex predicate array

Paramètres

  • predicate
    Type : 'T -> bool

    Fonction permettant de tester les éléments d'entrée.

  • array
    Type : 'T []

    Tableau d'entrée.

Exceptions

Exception

Condition

KeyNotFoundException

Levée si predicate ne retourne pas true pour un élément.

Valeur de retour

Index du premier élément du tableau qui répond au prédicat donné.

Notes

Cette fonction se nomme FindIndex dans les assemblys compilés. Si vous accédez à la fonction à partir d'un langage autre que F# ou par réflexion, utilisez ce nom.

Exemple

L'exemple suivant illustre l'utilisation de Array.find et de Array.findIndex pour identifier le premier entier supérieur à 1 qui est à la fois un carré et un cube.

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

Plateformes

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

Informations de version

Runtime F#

Pris en charge dans : 2.0, 4.0

Silverlight

Prise en charge dans : 3

Voir aussi

Référence

Collections.Array, module (F#)

Microsoft.FSharp.Collections, espace de noms (F#)

Array.find<'T>, fonction (F#)