As a little mini-project I wanted to count the number of lines of source code in the Second Edition of the Scala Cookbook as compared to the First Edition. To do this I wrote the following Scala program/script to count the lines between the ----
and ....
sections in the AsciiDoc files that represent the old and new versions of the book:
import java.io._
import scala.io.Source
object CountSourceCodeLines extends App {
val listOfFilenamesFile = "list_of_filenames"
// turn the “list of filenames” file into a list
val listOfFilenames = Source.fromFile(listOfFilenamesFile).getLines.toList
var linesOfSourceCode = 0
var inSourceCodeSection = false
// each filename is an Adoc file
for (filename <- listOfFilenames) {
val adocLines = Source.fromFile(filename).getLines.toList
for (line <- adocLines) {
if (line.matches("^----$") || line.matches("^\\.\\.\\.\\.$")) {
// if (line.matches("^\\.\\.\\.\\.$")) {
// matched a line like "-----" or "....", so toggle this
if (inSourceCodeSection==false)
inSourceCodeSection = true
else
inSourceCodeSection = false
} else {
if (inSourceCodeSection && line.trim != "" && line.trim.startsWith("//") == false) {
linesOfSourceCode += 1 // <-- what you really want
println(line) // <-- debug
}
}
}
}
println(s"TOTAL COUNT: $linesOfSourceCode")
}
I don’t show the listoffilenames file contents here, but it contains the AsciiDoc files for each book. (I rotate the contents of that file when I run this script.)
What I learned by running this code is that Chapter 11 of the Second Edition of the Scala Cookbook contains about 35% more examples than the First Edition contains. Adding more examples to the Cookbook was a goal of mine, and I’m glad to see that number that as high as it is. It’s important to add that currently the page count is exactly the same in each Edition.