List.tryPick<'T,'U> 함수(F#)

업데이트: 2010년 8월

지정된 함수를 연속 요소에 적용하여 함수가 일부 값에 대해 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 -> 'U option

    요소에서 옵션을 생성하는 함수입니다.

  • list
    형식: 'T list

    입력 목록입니다.

반환 값

첫 번째 결과 값 또는 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 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

버전 정보

F# 런타임

지원되는 버전: 2.0, 4.0

Silverlight

지원되는 버전: 3

참고 항목

참조

Collections.List 모듈(F#)

Microsoft.FSharp.Collections 네임스페이스(F#)

변경 기록

날짜

변경 내용

이유

2010년 8월

코드 예제를 추가했습니다.

향상된 기능 관련 정보