Share via


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

Applica la funzione specificata a elementi consecutivi, restituendo il primo risultato in cui la funzione restituisce Some. Se la funzione non restituisce Some per qualsiasi elemento, viene restituito None.

Percorso di spazio dei nomi/modulo: Microsoft.FSharp.Collections.Array

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

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

// Usage:
Array.tryPick chooser array

Parametri

  • chooser
    Tipo: 'T -> 'U option

    Funzione per trasformare gli elementi di matrice in opzioni.

  • array
    Tipo: 'T []

    Matrice di input.

Valore restituito

Il primo ha trasformato l'elemento che presenta un valore dell'opzione Some oppure None se la funzione non restituisce Some per qualsiasi elemento.

Note

Questa funzione è denominata TryPick negli assembly compilati. Utilizzare questo nome se si accede alla funzione da un linguaggio diverso da F# o tramite reflection.

Esempio

Nell'esempio riportato di seguito viene illustrato l'utilizzo di Array.tryPick per tentare di posizionare un elemento che soddisfi una condizione e vengono inoltre restituiti dati aggiuntivi relativi all'elemento, in questo caso la radice quadrata e la radice cubica.

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 |]
            

Piattaforme

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

Informazioni sulla versione

F# Runtime

Supportato in: 2.0, 4.0

Silverlight

Supportato in: 3

Vedere anche

Riferimenti

Modulo Collections.Array (F#)

Spazio dei nomi Microsoft.FSharp.Collections (F#)