Created
March 17, 2011 11:13
-
-
Save ikennaokpala/874170 to your computer and use it in GitHub Desktop.
Multiplication Table with Scala
This file contains 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
// 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