Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies the given function to each element of the collection.
Namespace/Module Path: Microsoft.FSharp.Collections.Seq
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
Seq.iter : ('T -> unit) -> seq<'T> -> unit
// Usage:
Seq.iter action source
Parameters
action
Type: 'T -> unitA function to apply to each element of the sequence.
source
Type: seq<'T>The input sequence.
Exceptions
Exception |
Condition |
---|---|
Thrown when the input sequence is null. |
Remarks
This function is named Iterate in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.
Example
The following example illustrates the use of Seq.iter.
printf "Seq.iter: "
Seq.iter (fun (a,b) -> printf "(%d, %d) " a b) (seq { for i in 1..5 -> (i, i*i) })
Seq.iter: (1, 1) (2, 4) (3, 9) (4, 16) (5, 25)
The following example illustrates the use of Seq.iter to work with CSV (Comma-Separated Value) files.
// Write a test file
System.IO.File.WriteAllLines(@"test.csv", [| "Desmond, Barrow, Market Place, 2";
"Molly, Singer, Band, 12" |]);
/// This function builds an IEnumerable<string list> object that enumerates the CSV-split
/// lines of the given file on-demand
let CSVFileEnumerator(fileName) =
// The function is implemented using a sequence expression
seq { use sr = System.IO.File.OpenText(fileName)
while not sr.EndOfStream do
let line = sr.ReadLine()
let words = line.Split [|',';' ';'\t'|]
yield words }
// Now test this out on our test file, iterating the entire file
let test = CSVFileEnumerator(@"test.csv")
printfn "-------Enumeration 1------";
test |> Seq.iter (string >> printfn "line %s");
// Now do it again, this time determining the numer of entries on each line.
// Note how the file is read from the start again, since each enumeration is
// independent.
printfn "-------Enumeration 2------";
test |> Seq.iter (Array.length >> printfn "line has %d entries");
// Now do it again, this time determining the numer of entries on each line.
// Note how the file is read from the start again, since each enumeration is
// independent.
printfn "-------Enumeration 3------";
test |> Seq.iter (Array.map (fun s -> s.Length) >> printfn "lengths of entries: %A")
-------Enumeration 1------ line System.String[] line System.String[] -------Enumeration 2------ line has 8 entries line has 7 entries -------Enumeration 3------ lengths of entries: [|7; 0; 6; 0; 6; 5; 0; 1|] lengths of entries: [|5; 0; 6; 0; 4; 0; 2|]
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