Udostępnij za pośrednictwem


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

Applies the given function to pair of elements drawn from matching indices in two arrays. The two arrays must have the same lengths, otherwise an ArgumentException is raised.

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

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

// Signature:
Array.iter2 : ('T1 -> 'T2 -> unit) -> 'T1 [] -> 'T2 [] -> unit

// Usage:
Array.iter2 action array1 array2

Parameters

  • action
    Type: 'T1 -> 'T2 -> unit

    The function to apply.

  • 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.

Remarks

This function is named Iterate2 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 example shows the differences between Array.iter, Array.iter2, Array.iteri, and Array.iteri2.

let array1 = [| 1; 2; 3 |]
let array2 = [| 4; 5; 6 |]
Array.iter (fun x -> printfn "Array.iter: element is %d" x) array1
Array.iteri(fun i x -> printfn "Array.iteri: element %d is %d" i x) array1
Array.iter2 (fun x y -> printfn "Array.iter2: elements are %d %d" x y) array1 array2
Array.iteri2 (fun i x y ->
               printfn "Array.iteri2: element %d of array1 is %d element %d of array2 is %d"
                 i x i y)
            array1 array2

Output

Array.iter: element is 1
Array.iter: element is 2
Array.iter: element is 3
Array.iteri: element 0 is 1
Array.iteri: element 1 is 2
Array.iteri: element 2 is 3
Array.iter2: elements are 1 4
Array.iter2: elements are 2 5
Array.iter2: elements are 3 6
Array.iteri2: element 0 of array1 is 1 element 0 of array2 is 4
Array.iteri2: element 1 of array1 is 2 element 1 of array2 is 5
Array.iteri2: element 2 of array1 is 3 element 2 of array2 is 6

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

Microsoft.FSharp.Collections Namespace (F#)