Seq.iteri<'T> Function (F#)

Applies the given function to each element of the collection. The integer passed to the function indicates the index of element.

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

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

// Signature:
Seq.iteri : (int -> 'T -> unit) -> seq<'T> -> unit

// Usage:
Seq.iteri action source

Parameters

  • action
    Type: int -> 'T -> unit

    A function to apply to each element of the sequence that can also access the current index.

  • source
    Type: seq<'T>

    The input sequence.

Exceptions

Exception

Condition

ArgumentNullException

Thrown when the input sequence is null.

Remarks

This function is named IterateIndexed in the .NET assembly. If accessing the member from a .NET language other than F#, or through reflection, use this name.

Example

The following code shows how to use Seq.iteri and compares its behavior to related functions.

let seq1 = [1; 2; 3]
let seq2 = [4; 5; 6]
Seq.iter (fun x -> printfn "Seq.iter: element is %d" x) seq1
Seq.iteri(fun i x -> printfn "Seq.iteri: element %d is %d" i x) seq1
Seq.iter2 (fun x y -> printfn "Seq.iter2: elements are %d %d" x y) seq1 seq2

Output

Seq.iter: element is 1
Seq.iter: element is 2
Seq.iter: element is 3
Seq.iteri: element 0 is 1
Seq.iteri: element 1 is 2
Seq.iteri: element 2 is 3
Seq.iter2: elements are 1 4
Seq.iter2: elements are 2 5
Seq.iter2: elements are 3 6

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

Microsoft.FSharp.Collections Namespace (F#)