Created
May 6, 2021 09:49
-
-
Save raspasov/156da3ff7e2fb6a8b35221c0a28dc8af to your computer and use it in GitHub Desktop.
Count comment lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns ss.experimental.comment-line-count | |
(:require [net.cgrand.xforms :as x])) | |
(defn count-comment-lines [] | |
(let [source (slurp "/Users/raspasov/cpp") | |
lines (line-seq | |
(java.io.BufferedReader. | |
(java.io.StringReader. source)))] | |
(transduce | |
(comp | |
(partition-by | |
(fn [line] | |
(cond | |
(clojure.string/includes? line "/*") :start | |
(clojure.string/includes? line "*/") :end))) | |
;partition so we can access previous elements | |
(x/partition 2 1 (x/into [])) | |
(map | |
(fn [[[?comment-start-line] ?comment-lines :as v]] | |
(if (clojure.string/includes? ?comment-start-line "/*") | |
;return number of commented lines | |
(count ?comment-lines) | |
;else, return vector unchanged | |
v))) | |
;only the numbers | |
(filter number?)) | |
;sum all the commented lines | |
+ | |
lines))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment