Created
May 12, 2012 20:50
-
-
Save razorjack/2668943 to your computer and use it in GitHub Desktop.
Pattern Matching in 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
import java.util.Date | |
import java.text.DateFormat | |
import java.text.DateFormat._ | |
val echoMath = """^echo (.*)""".r | |
val countMath = """^count from (\d) to (\d)""".r | |
while (true) { | |
print("> ") // prompt | |
scala.io.Source.stdin.getLine(0) match { | |
case "hello" => | |
println("hi") | |
case "time" => | |
val currentDate = new Date | |
val df = getDateInstance(LONG) | |
println(df format currentDate) | |
case echoMath(str) => | |
println(str) | |
case countMath(a, b) => | |
val m = a.toInt | |
val n = b.toInt | |
for (i <- (m to n)) { | |
print(i + ", ") | |
} | |
println("boom!") | |
case "exit" => | |
println("bye") | |
sys.exit | |
case _ => println("command not matched") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example session: