openSystem.IOopenSystemletgetFilesdirectory=matchDirectory.Existsdirectorywith|true ->Directory.GetFiles(directory)|>Option.Some|false ->NoneletcountLines(file:string)=usereader=newStreamReader(file)letcontent=reader.ReadToEnd()content.ToCharArray()|>Array.fold(funstate item->if item='\n'then state + 1else state + 0)0letprintReportdirectory=matchdirectory|>getFileswith|None->printfn"Couldn'tgetanyfilesfromthedirectory."|Somefiles->letlineCounts=files|>Array.map(funx->countLinesx)Array.iter2(funxy->printfn"File:%s - %ilines"xy)fileslineCounts//Exampleusageprintfn"Enteradirectory"Console.ReadLine()|>printReport
Comments (2)
chosen breed
I like it! I've not dealt much with the Array module but this looks good. There are probably a number of ways to count lines. This seems as good as any. You gotta' love the "use" construct. I ran it in F# Interactive and works a treat :-)
Cameron Presley
Totally! The fact that use takes care of disposing is awesome. Thanks for the feedback.
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.
I like it! I've not dealt much with the Array module but this looks good. There are probably a number of ways to count lines. This seems as good as any. You gotta' love the "use" construct. I ran it in F# Interactive and works a treat :-)
Totally! The fact that use takes care of disposing is awesome. Thanks for the feedback.