Skip to content

Instantly share code, notes, and snippets.

@msbaek
Last active August 29, 2015 14:20
Show Gist options
  • Save msbaek/76c73ab700a0f950998a to your computer and use it in GitHub Desktop.
Save msbaek/76c73ab700a0f950998a to your computer and use it in GitHub Desktop.
Printing formatted character counts for the lines of a url
// Listing 3.11 Printing formatted character counts for the lines of a url
import scala.io.Source
def widthOfLength(s: String) = s.length.toString.length
val lines = Source.fromURL("https://gist.githubusercontent.com/msbaek/e830f66f5165fbbb844e/raw/d48da60bb3479f5f708367e677a492462afcf66e/gistfile1.scala", "utf8").getLines().toList
val longestLine = lines.reduceLeft(
(a, b) => if(a.length > b.length) a else b
)
val maxWidth = widthOfLength(longestLine)
for(line <- lines) {
val numSpaces = maxWidth - widthOfLength(line)
val padding = " " * numSpaces
println(padding + line.length + " | " + line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment