共用方式為


Seq.delay<'T> 函式 (F#)

傳回序列,這個序列是從指定的延遲序列規格所建置。

命名空間/模組路徑: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 8 中, Windows 7, Windows Server 2012 上, Windows Server 2008 R2

版本資訊

F# 核心程式庫版本

支援版本:2.0, 4.0,可攜式執行檔 (PE)。

請參閱

參考

Collections.Seq 模組 (F#)

Microsoft.FSharp.Collections 命名空間 (F#)