Condividi tramite


Array.map2<'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. 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.map2 : ('T1 -> 'T2 -> 'U) -> 'T1 [] -> 'T2 [] -> 'U []

// Usage:
Array.map2 mapping array1 array2

Parameters

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

    The function to transform the pairs of the input elements.

  • 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 Map2 in compiled assemblies. If you are accessing the function from a .NET language other than F#, or through reflection, use this name.

Example

The following code example shows the use of Array.map2.

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

Output

[|5; 7; 9|]

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#)