Funzione Seq.iter<'T> (F#)

Applica la funzione specificata a ogni elemento della raccolta.

Percorso spazio dei nomi/modulo: Microsoft.FSharp.Collections.Seq

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

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

// Usage:
Seq.iter action source

Parametri

  • action
    Tipo: 'T -> unit

    Funzione da applicare a ogni elemento della sequenza.

  • source
    Tipo: seq<'T>

    Sequenza di input.

Eccezioni

Eccezione

Condizione

ArgumentNullException

Generata se la sequenza di input è Null.

Note

Questa funzione è denominata Iterate negli assembly compilati. Utilizzare questo nome se si accede alla funzione da un linguaggio diverso da F# o tramite reflection.

Esempio

Nell'esempio seguente viene illustrato l'utilizzo di Seq.iter.

printf "Seq.iter: "
Seq.iter (fun (a,b) -> printf "(%d, %d) " a b) (seq { for i in 1..5 -> (i, i*i) })
  

Nell'esempio seguente viene illustrato l'utilizzo di Seq.iter con i file CSV (Comma-Separated Value).

// 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")
  

Piattaforme

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

Informazioni sulla versione

Versioni della libreria di base F#

Supportato in: 2,0, 4,0, portabile

Vedere anche

Riferimenti

Modulo Collections.Seq (F#)

Spazio dei nomi Microsoft.FSharp.Collections (F#)