다음을 통해 공유


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

Creates an array given the dimension and a generator function to compute the elements.

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

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

// Signature:
Array.init : int -> (int -> 'T) -> 'T []

// Usage:
Array.init count initializer

Parameters

  • count
    Type: int

    The number of elements to initialize.

  • initializer
    Type: int -> 'T

    The function to generate the initial values for each index.

Return Value

The created array.

Remarks

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.

Example

The following code demonstrates the use of Array.init.

printfn "Array of squares: %A" (Array.init 10 (fun index -> index * index))
Array of squares: [|0; 1; 4; 9; 16; 25; 36; 49; 64; 81|]

Platforms

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

Version Information

F# Core Library Versions

Supported in: 2.0, 4

See Also

Reference

Collections.Array Module (F#)

Microsoft.FSharp.Collections Namespace (F#)