Compartilhar via


Array.mapi2<'T1,'T2,'U> Function (F#)

Builds a new collection whose elements are the results of applying the given function to the corresponding elements of the two collections pairwise, also passing the index of the elements. The two input arrays must have the same lengths, otherwise ArgumentException is raised.

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

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

// Signature:
Array.mapi2 : (int -> 'T1 -> 'T2 -> 'U) -> 'T1 [] -> 'T2 [] -> 'U []

// Usage:
Array.mapi2 mapping array1 array2

Parameters

  • mapping
    Type: int -> 'T1 -> 'T2 -> 'U

    The function to transform pairs of input elements and their indices.

  • array1
    Type: 'T1 []

    The first input array.

  • array2
    Type: 'T2 []

    The second input array.

Exceptions

Exception

Condition

ArgumentException

Thrown when the input arrays differ in length.

Return Value

The array of transformed elements.

Remarks

This function is named MapIndexed2 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.mapi2.

let array1 = [| 1; 2; 3 |]
let array2 = [| 4; 5; 6 |]
let arrayAddTimesIndex = Array.mapi2 (fun i x y -> (x + y) * i) array1 array2
printfn "%A" arrayAddTimesIndex

Output

[|0; 7; 18|]

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.Array Module (F#)

Microsoft.FSharp.Collections Namespace (F#)