다음을 통해 공유


Seq.delay<'T> 함수(F#)

업데이트: 2010년 8월

시퀀스의 지정된 지연 사양에서 빌드된 시퀀스를 반환합니다.

네임스페이스/모듈 경로: Microsoft.FSharp.Collections.Seq

어셈블리: FSharp.Core(FSharp.Core.dll)

// Signature:
Seq.delay : (unit -> seq<'T>) -> seq<'T>

// Usage:
Seq.delay generator

매개 변수

  • generator
    형식: unit -> seq<'T>

    시퀀스에 대한 생성 함수입니다.

반환 값

결과 시퀀스입니다.

설명

입력 함수는 시퀀스의 IEnumerator가 요청될 때마다 실행됩니다.

컴파일된 어셈블리에서 이 함수의 이름은 Delay입니다. F# 이외의 언어에서 함수에 액세스하거나 리플렉션을 통해 함수에 액세스하는 경우 이 이름을 사용합니다.

예제

다음 코드는 Seq.delay를 사용하여 일반적으로 즉시 평가되는 컬렉션에서 만들어진 시퀀스의 평가를 지연시키는 방법에 대해 보여줍니다.

// Normally sequences are evaluated lazily.  In this case,
// the sequence is created from a list, which is not evaluated
// lazily. Therefore, without Seq.delay, the elements would be
// evaluated at the time of the call to makeSequence.
let makeSequence function1 maxNumber = Seq.delay (fun () ->
    let rec loop n acc =
        printfn "Evaluating %d." n
        match n with
        | 0 -> acc
        | n -> (function1 n) :: loop (n - 1) acc
    loop maxNumber []
    |> Seq.ofList)
printfn "Calling makeSequence."
let seqSquares = makeSequence (fun x -> x * x) 4          
let seqCubes = makeSequence (fun x -> x * x * x) 4
printfn "Printing sequences."
printfn "Squares:"
seqSquares |> Seq.iter (fun x -> printf "%d " x)
printfn "\nCubes:"
seqCubes |> Seq.iter (fun x -> printf "%d " x)                       

Output

                            

다음 코드 예제는 Seq.delay를 사용하지 않는다는 것을 제외하고 이전 예제와 동일합니다. 출력이 서로 다른 것을 확인할 수 있습니다.

// Compare the output of this example with that of the previous.
// Notice that Seq.delay delays the
// execution of the loop until the sequence is used.
let makeSequence function1 maxNumber =
    let rec loop n acc =
        printfn "Evaluating %d." n
        match n with
        | 0 -> acc
        | n -> (function1 n) :: loop (n - 1) acc
    loop maxNumber []
    |> Seq.ofList
printfn "Calling makeSequence."
let seqSquares = makeSequence (fun x -> x * x) 4          
let seqCubes = makeSequence (fun x -> x * x * x) 4
printfn "Printing sequences."
printfn "Squares:"
seqSquares |> Seq.iter (fun x -> printf "%d " x)
printfn "\nCubes:"
seqCubes |> Seq.iter (fun x -> printf "%d " x)

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.Seq 모듈(F#)

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

변경 기록

날짜

변경 내용

이유

2010년 8월

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

향상된 기능 관련 정보