Skip to content

Instantly share code, notes, and snippets.

@raspasov
Created May 6, 2021 09:49
Show Gist options
  • Save raspasov/156da3ff7e2fb6a8b35221c0a28dc8af to your computer and use it in GitHub Desktop.
Save raspasov/156da3ff7e2fb6a8b35221c0a28dc8af to your computer and use it in GitHub Desktop.
Count comment lines
(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