Seq.windowed<'T> Function (F#)
Returns a sequence that yields sliding windows of containing elements drawn from the input sequence. Each window is returned as a fresh array.
Namespace/Module Path: Microsoft.FSharp.Collections.Seq
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature: Seq.windowed : int -> seq<'T> -> seq<'T []> // Usage: Seq.windowed windowSize source
The following example demonstrates the use of Seq.windowed as part of a computation of a moving average for a sequence of numbers.
let seqNumbers = [ 1.0; 1.5; 2.0; 1.5; 1.0; 1.5 ] :> seq<float> let seqWindows = Seq.windowed 3 seqNumbers let seqMovingAverage = Seq.map Array.average seqWindows printfn "Initial sequence: " printSeq seqNumbers printfn "\nWindows of length 3: " printSeq seqWindows printfn "\nMoving average: " printSeq seqMovingAverage
Initial sequence: 1.0 1.5 2.0 1.5 1.0 1.5 Windows of length 3: [|1.0; 1.5; 2.0|] [|1.5; 2.0; 1.5|] [|2.0; 1.5; 1.0|] [|1.5; 1.0; 1.5|] Moving average: 1.5 1.666666667 1.5 1.333333333