Поделиться через


Функция List.tryPick<'T,'U> (F#)

Применяет заданную функцию к идущим подряд элементам, возвращая первый результат, когда функция возвращает Some для некоторого значения.Если такого элемента нет, возвращает значение None.

Пространство имен/путь к модулю: Microsoft.FSharp.Collections.List

Сборка: FSharp.Core (в FSharp.Core.dll)

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

// Usage:
List.tryPick chooser list

Параметры

  • chooser
    Тип: 'T -> 'Uoption

    Функция для создания параметров из элементов.

  • list
    Тип: 'Tlist

    Входной список.

Возвращаемое значение

Первое результирующее значение или None.

Заметки

В скомпилированных сборках имя этой функции — TryPick.При обращении к функции из языка, отличного от F#, или посредством отражения следует использовать это имя.

Пример

В следующем примере кода показано, как использовать 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 ]

Output

  
  
  
  
  

Платформы

Windows 8, Windows 7, Windows Server 2012, Windows 2008 Server R2

Сведения о версии

Основной версии библиотеки F#

Поддерживается в: 2.0, 4.0, портативное

См. также

Ссылки

Модуль Collections.List (F#)

Пространство имен Microsoft.FSharp.Collections (F#)