Can you improve this 'lines of code algorithm' in F#?

前端 未结 3 1612
一生所求
一生所求 2021-01-21 02:59

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,

相关标签:
3条回答
  • 2021-01-21 03:05

    A better site for this might be refactormycode - it's tailored exactly for these questions.

    0 讨论(0)
  • 2021-01-21 03:15

    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.

    0 讨论(0)
  • 2021-01-21 03:28

    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.

    0 讨论(0)
提交回复
热议问题