如何获得有关我的F#代码的一些不错的统计数据?
我可以想象像
这样的东西答案 0 :(得分:10)
为什么不使用一些简单的shell实用程序?
按顺序回答
wc -l *.fs
ls -l *.fs | wc -l
wc -c *.fs
grep module *.fs | wc -l
grep type *.fs | wc -l
grep "^let\|member" *.fs | wc -l
更新:递归文件夹的一些示例 - 我希望模式很明显
wc -l `find . -name "*.fs" `
find . -name "*.fs" | wc -l
wc -c `find . -name "*.fs" `
grep module `find . -name "*.fs" ` | wc -l
答案 1 :(得分:4)
这是一个F#版本,递归计算fs
和fsx
个文件(假设你安装了F#运行时):
open System.IO
open System.Text.RegularExpressions
let rec allFiles dir =
seq { yield! Directory.GetFiles dir
yield! Seq.collect allFiles (Directory.GetDirectories dir) }
let rgType = new Regex(@"type", RegexOptions.Compiled)
let rgModule = new Regex(@"module", RegexOptions.Compiled)
let rgFunction = new Regex(@"^let|member", RegexOptions.Compiled)
let count (rg: Regex) s =
s |> rg.Matches |> fun x -> x.Count
type Statistics = {
NumOfLines: int; NumOfFiles: int;
NumOfChars: int; NumOfTypes: int;
NumOfModules: int; NumOfFunctions: int;
}
let getStats =
allFiles
>> Seq.filter (fun f -> f.EndsWith ".fs" || f.EndsWith ".fsx")
>> Seq.fold (fun acc f ->
let contents = f |> File.ReadLines
{ NumOfLines = acc.NumOfLines + Seq.length contents;
NumOfFiles = acc.NumOfFiles + 1;
NumOfChars = acc.NumOfChars + Seq.sumBy String.length contents;
NumOfTypes = acc.NumOfTypes + Seq.sumBy (count rgType) contents;
NumOfModules = acc.NumOfModules + Seq.sumBy (count rgModule) contents;
NumOfFunctions = acc.NumOfFunctions + Seq.sumBy (count rgFunction) contents; }
) { NumOfLines = 0; NumOfFiles = 0;
NumOfChars = 0; NumOfTypes = 0;
NumOfModules = 0; NumOfFunctions = 0 }
答案 2 :(得分:0)
因为F#编译为IL代码和公共类型系统(CTS)和PDB files,所以工具NDepend可以分析F#程序集,并且您将得到关于代码的82 code metrics( +所有others features of the tool)。提供免费试用版here。
免责声明,我是NDepend团队的成员。