Condividi tramite


Funzione Seq.delay<'T> (F#)

Restituisce una sequenza compilata a partire dalla specifica ritardata indicata di una sequenza.

Percorso spazio dei nomi/modulo: Microsoft.FSharp.Collections.Seq

Assembly: FSharp.Core (in FSharp.Core.dll)

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

// Usage:
Seq.delay generator

Parametri

  • generator
    Tipo: unit -> seq<'T>

    Funzione generatrice per la sequenza.

Valore restituito

Sequenza risultante.

Note

La funzione di input viene valutata ogni volta che viene richiesto un IEnumerator per la sequenza.

Questa funzione è denominata Delay negli assembly compilati. Utilizzare questo nome se si accede alla funzione da un linguaggio diverso da F# o tramite reflection.

Esempio

Nel codice seguente viene illustrato come utilizzare Seq.delay per ritardare la valutazione di una sequenza creata da un insieme che in genere viene valutato immediatamente.

// 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

                            

L'esempio di codice seguente è equivalente all'esempio precedente, eccetto che non utilizza Seq.delay. Notare la differenza nell'output.

// 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

                            

Piattaforme

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2.

Informazioni sulla versione

F# Runtime

Supportato in: 2.0, 4.0

Silverlight

Supportato in: 3

Vedere anche

Riferimenti

Modulo Collections.Seq (F#)

Spazio dei nomi Microsoft.FSharp.Collections (F#)

Cronologia delle modifiche

Data

Cronologia

Motivo

Agosto 2010

Aggiunto esempio di codice.

Miglioramento delle informazioni.