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

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

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

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

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

// Usage:
Seq.init count initializer

Parameters

  • count
    Type: int

    The maximum number of items to generate for the sequence.

  • initializer
    Type: int -> 'T

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

Exceptions

Exception

Condition

ArgumentException

Thrown when count is negative.

Return Value

The result sequence.

Remarks

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.

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

Thread Safety

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

Example

The following example demonstrates the use of Seq.init to create a sequence of the first five multiples of 10.

let seqFirst5MultiplesOf10 = Seq.init 5 (fun n -> n * 10)
Seq.iter (fun elem -> printf "%d " elem) seqFirst5MultiplesOf10
0 10 20 30 40

Platforms

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

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)