|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
Seq.delay<'T>, fonction (F#)
// Signature: Seq.delay : (unit -> seq<'T>) -> seq<'T> // Usage: Seq.delay generator
// 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)
Sortie
Appel de makeSequence.
Impression de séquences
Carrés : évaluer 4.
Évaluation 3.
Évaluation 2.
Évaluation 1.
Évaluation 0.
16 9 4 cubes 1 : évaluer 4.
Évaluation 3.
Évaluation 2.
Évaluation 1.
Évaluation 0.
64 27 8 1
// 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)
Sortie
Appel de makeSequence.
Évaluation 4.
Évaluation 3.
Évaluation 2.
Évaluation 1.
Évaluation 0.
Évaluation 4.
Évaluation 3.
Évaluation 2.
Évaluation 1.
Évaluation 0.
Impression de séquences
Carrés : 16 9 4 cubes 1 : 64 27 8 1