【发布时间】:2015-05-03 10:47:03
【问题描述】:
我编写了一个小脚本来遍历文件夹中的文件以计算代码行数。
脚本的核心是计算空格、cmets 和代码行数的函数。 (请注意,目前它是为 C# 量身定制的,不了解多行 cmets)。
它对我来说看起来不太好 - 有人有更清洁的版本吗?
// from list of strings return tuple with count of (whitespace, comments, code)
let loc (arr:List<string>) =
let innerloc (whitesp, comment, code) (l:string) =
let s = l.Trim([|' ';'\t'|]) // remove leading whitespace
match s with
| "" -> (whitesp + 1, comment, code) //blank lines
| "{" -> (whitesp + 1, comment, code) //opening blocks
| "}" -> (whitesp + 1, comment, code) //closing blocks
| _ when s.StartsWith("#") -> (whitesp + 1, comment, code) //regions
| _ when s.StartsWith("//") -> (whitesp, comment + 1, code) //comments
| _ -> (whitesp, comment, code + 1)
List.fold_left innerloc (0,0,0) arr
【问题讨论】: