Compartilhar via


List.tryPick < T','U > Função (F#)

Aplica a função de determinado elementos sucessivas, retornando o primeiro resultado onde a função retornará Some para algum valor. Se não há tal elemento existe volte None.

Caminho do namespace/módulo: Microsoft.FSharp.Collections.List

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

// Signature:
List.tryPick : ('T -> 'U option) -> 'T list -> 'U option

// Usage:
List.tryPick chooser list

Parâmetros

  • chooser
    Tipo: 'T -> 'U opção

    A função para gerar as opções dos elementos.

  • list
    Tipo: 'T lista

    A lista de entrada.

Valor de retorno

O primeiro valor resultante ou None.

Comentários

Esta função é chamada de TryPick em módulos (assemblies) compilados. Se você estiver acessando a função de um idioma diferente, por exemplo, F# ou através de reflexão, use esse nome.

Exemplo

O exemplo de código a seguir mostra como usar List.tryPick.

let findPerfectSquareAndCube list1 =
    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 List.tryPick testElement list1 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 ]

Saída

            

Plataformas

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

Informações sobre versão

O tempo de execução F#

Compatível com: 2.0, 4.0

Silverlight

Compatível com: 3

Consulte também

Referência

Módulo de Collections.List (F#)

Microsoft.FSharp.Collections Namespace (F#)

Histórico de alterações

Date

History

Motivo

Agosto de 2010

Exemplo de código adicionado.

Aprimoramento de informações.