Last active
August 29, 2015 14:20
-
-
Save msbaek/76c73ab700a0f950998a to your computer and use it in GitHub Desktop.
Printing formatted character counts for the lines of a url
This file contains hidden or 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
// 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