Skip to content

Instantly share code, notes, and snippets.

@ikennaokpala
Created March 17, 2011 11:13
Show Gist options
  • Save ikennaokpala/874170 to your computer and use it in GitHub Desktop.
Save ikennaokpala/874170 to your computer and use it in GitHub Desktop.
Multiplication Table with Scala
// Taken from the programming in Scala book second edition page 138
// Returns a row as a sequence
def makeRowSeq(row: int) =
for (col <- 1 to 10) yield {
val prod = (row * col).toString
val padding = " " *(4 - prod.length)
padding + prod
}
// Returns a row as a string
def makeRow(row: int) = makeRowSeq(row).mkString
// Returns a table as a string with one row per line
def multiTable () = {
val tableSeq = // a sequence of row strings
for (row <- 1 to 10)
yield makeRow(row)
tableSeq.mkString("\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment