Expand Minimize

Seq.initInfinite<'T> Function (F#)

Generates a new sequence which, when iterated, will return successive elements by calling the given function.

Namespace/Module Path: Microsoft.FSharp.Collections.Seq

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

// Signature:
Seq.initInfinite : (int -> 'T) -> seq<'T>

// Usage:
Seq.initInfinite initializer

initializer

Type: int -> 'T

A function that generates an item in the sequence from a given index.

The result sequence.

The results of calling the function will not be saved, that is the function will be reapplied as necessary to regenerate the elements. The function is passed the index of the item being generated.

Iteration can continue up to Int32.MaxValue.

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

The returned sequence may be passed between threads safely. However, individual IEnumerator values generated from the returned sequence should not be accessed concurrently.

The following example shows the use of Seq.initInfinite to create a sequence 1/n^2, with alternating signs.

let seqInfinite = Seq.initInfinite (fun index ->
    let n = float( index + 1 )
    1.0 / (n * n * (if ((index + 1) % 2 = 0) then 1.0 else -1.0)))
printfn "%A" seqInfinite
seq [-1.0; 0.25; -0.1111111111; 0.0625; ...]

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

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft