I\'ve written a little script to iterate across files in folders to count lines of code.
The heart of the script is this function to count lines of whitespace, comments,
A better site for this might be refactormycode - it's tailored exactly for these questions.
Can't see much wrong with that other than the fact you will count a single brace with trailing spaces as code instead of whitespace.
I think what you have is fine, but here's some variety to mix it up. (This solution repeats your problem of ignoring trailing whitespace.)
type Line =
| Whitespace = 0
| Comment = 1
| Code = 2
let Classify (l:string) =
let s = l.TrimStart([|' ';'\t'|])
match s with
| "" | "{" | "}" -> Line.Whitespace
| _ when s.StartsWith("#") -> Line.Whitespace
| _ when s.StartsWith("//") -> Line.Comment
| _ -> Line.Code
let Loc (arr:list<_>) =
let sums = Array.create 3 0
arr
|> List.iter (fun line ->
let i = Classify line |> int
sums.[i] <- sums.[i] + 1)
sums
"Classify" as a separate entity might be useful in another context.