Partager via


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

Parameters

  • windowSize
    Type: int

    The number of elements in each window.

  • source
    Type: seq<'T>

    The input sequence.

Exceptions

Exception

Condition

ArgumentException

Thrown when the input sequence is empty.

ArgumentNullException

Thrown when the input sequence is null.

Return Value

The result sequence.

Remarks

This function is named Windowed in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

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 

Platforms

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Reference

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)