Array.find<'T>-Funktion (F#)

Gibt das erste Element zurück, für das die angegebene Funktion true zurückgibt. Lösen Sie KeyNotFoundException aus, wenn kein solches Element vorhanden ist.

Namespace/Modulpfad: Microsoft.FSharp.Collections.Array

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

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

// Usage:
Array.find predicate array

Parameter

  • predicate
    Typ: 'T -> bool

    Die Funktion zum Testen der Eingabeelemente.

  • array
    Typ: 'T []

    Das Eingabearray.

Ausnahmen

Ausnahme

Bedingung

KeyNotFoundException

Wird ausgelöst, wenn predicate nicht true für ein Element zurückgibt.

Rückgabewert

Das erste Element, für das predicate true zurückgibt.

Hinweise

Der Name dieser Funktion in kompilierten Assemblys lautet Find. Verwenden Sie diesen Namen, wenn Sie in einer anderen .NET-Sprache als F# oder durch Reflektion auf die Funktion zugreifen.

Beispiel

Das folgende Beispiel veranschaulicht die Verwendung von Array.find und Array.findIndex, um die erste ganze Zahl zu ermitteln, die größer als 1 ist und sowohl ein Quadrat als auch ein Cube darstellt.

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
  

Plattformen

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

Versionsinformationen

F#-Runtime

Unterstützt in: 2.0, 4.0

Silverlight

Unterstützt in: 3

Siehe auch

Weitere Ressourcen

Collections.Array-Modul (F#)

Microsoft.FSharp.Collections-Namespace (F#)

Array.findIndex<'T>-Funktion (F#)