Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

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
action

Type: 'T1 -> 'T2 -> unit

The function to apply.

array1

Type: 'T1 []

The first input array.

array2

Type: 'T2 []

The second input array.

Exception

Condition

ArgumentException

Thrown when the input arrays differ in length.

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.

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

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

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.