共用方式為


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)                       

輸出

                            

下列程式碼範例是先前的範例相同,不同之處在於它並不會使用 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)

輸出

                            

平台

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#)

變更記錄

日期

History

原因

2010 年 8 月

加入程式碼範例。

資訊加強。