Last active
December 20, 2015 21:39
-
-
Save lu911/6199221 to your computer and use it in GitHub Desktop.
Pattern Matching
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
object Main { | |
def main(args: Array[String]): Unit = { | |
new PatternMatching() | |
} | |
} | |
class PatternMatching{ | |
val methods = List(_match, _match2, guard, patternVar, patternGuardVar, typePattern, arrMatch, lstMatch, pairMatch, caseClass, optionMatch, partialFunction) | |
def _match(): Unit = { | |
var sign = 0 | |
val ch: Char = '+' | |
ch match { | |
case '+' => sign = 1 | |
case '-' => sign = 2 | |
} | |
println("-"*5 + " default match " + "-"*5) | |
println("sign : " + sign) | |
} | |
def _match2(): Unit = { | |
val ch: Char = 'a' | |
val sign = ch match { | |
case '+' => 1 | |
case '-' => 2 | |
case _ => 0 | |
} | |
println("-"*5 + " default match2 " + "-"*5) | |
println("sign : " + sign) | |
} | |
def guard(): Unit = { | |
val ch: Char = '5' | |
val sign = ch match { | |
case '+' => -ch | |
case '-' => -ch | |
case _ if Character.isDigit(ch) => "guard" | |
case _ => 0 | |
} | |
println("-"*5 + " guard match " + "-"*5) | |
println("sign : " + sign) | |
} | |
def patternVar(): Unit = { | |
val str = "10+20+30" | |
val ch: Char = '+' | |
val sign = str(3) match { | |
case '-' => -1 | |
case ch => "pattern var" | |
} | |
println("-"*5 + " pattern var match " + "-"*5) | |
println("sign : " + sign) | |
} | |
def patternGuardVar(): Unit = { | |
val str = "10+20+30" | |
val ch: Char = '+' | |
val sign = str(1) match { | |
case '-' => -1 | |
case ch if Character isDigit(ch) => "pattern guard var" | |
} | |
println("-"*5 + " pattern guard var match " + "-"*5) | |
println("sign : " + sign) | |
} | |
def typePattern(): Unit = { | |
def typeMatcher(obj: Any): Any = obj match { | |
case i:Int => i | |
case s:String => Integer.parseInt(s) | |
case _: BigInt => Int.MaxValue | |
} | |
println("-"*5 + " type match " + "-"*5) | |
println("typeMatcher(1) : " + typeMatcher(1)) | |
println("typeMatcher(\"1\") : " + typeMatcher("1")) | |
println("typeMatcher(1:BigInt) : " + typeMatcher(1:BigInt)) | |
} | |
def arrMatch(): Unit = { | |
def typeMatcher(obj: Any): Any = obj match { | |
case Array(0) => "0" | |
case Array(x,y) => x + ", " + y | |
case Array(0, _*) => "0 ..." | |
case _ => "something else" | |
} | |
println("-"*5 + " Array match " + "-"*5) | |
println("typeMatcher(Array(0)) : " + typeMatcher(Array(0))) | |
println("typeMatcher(Array(1,2)) : " + typeMatcher(Array(1,2))) | |
println("typeMatcher(Array(0,1,2,3)) : " + typeMatcher(Array(0,1,2,3))) | |
println("typeMatcher(Array(3,2,1)) : " + typeMatcher(Array(3,2,1))) | |
} | |
def lstMatch(): Unit = { | |
def typeMatcher(obj: Any): Any = obj match { | |
case 0 :: Nil => "0" | |
case x :: y :: Nil => x + ", " + y | |
case 0 :: tail => "0 ..." | |
case _ => "something else" | |
} | |
println("-"*5 + " List match " + "-"*5) | |
println("typeMatcher(List(0)) : " + typeMatcher(List(0))) | |
println("typeMatcher(List(1,2)) : " + typeMatcher(List(1,2))) | |
println("typeMatcher(List(0,1,2,3)) : " + typeMatcher(List(0,1,2,3))) | |
println("typeMatcher(List(3,2,1)) : " + typeMatcher(List(3,2,1))) | |
} | |
def pairMatch(): Unit = { | |
def typeMatcher(obj: Any): Any = obj match { | |
case (0, _) => "0 ..." | |
case (y,0) => y + " 0" | |
case _ => "neither is 0" | |
} | |
println("-"*5 + " pair match " + "-"*5) | |
println("typeMatcher((0,0)) : " + typeMatcher((0,0))) | |
println("typeMatcher((1,0)) : " + typeMatcher((1,0))) | |
println("typeMatcher((1,2,3)) : " + typeMatcher((1,2,3))) | |
} | |
def caseClass(): Unit = { | |
abstract class Amount | |
case class Doller(value: Double) extends Amount | |
case class Currency(value: Double, unit: String) extends Amount | |
case object Nothing extends Amount | |
def typeMatcher(obj: Amount): Any = obj match { | |
case Doller(v) => "$" + v | |
case Currency(_, u) => "Oh noes, I got " + u | |
case Nothing => "nothing" | |
} | |
println("-"*5 + " case class match " + "-"*5) | |
println("typeMatcher(Doller(5)) : " + typeMatcher(Doller(5))) | |
println("typeMatcher(Currency(5, \"EUR\")) : " + typeMatcher(Currency(5, "EUR"))) | |
println("typeMatcher((Nothing)) : " + typeMatcher((Nothing))) | |
} | |
def optionMatch(): Unit = { | |
val scores = Map(("Alice",10)) | |
println("-"*5 + " option match " + "-"*5) | |
scores.get("Alice") match { | |
case Some(score) => println(score) | |
case None => println("No score") | |
} | |
val aliceScore = scores.get("Alice") | |
if(aliceScore.isEmpty) println("No score") | |
else println(aliceScore.get) | |
println(scores.getOrElse("Alice", "No score")) | |
} | |
def partialFunction(): Unit = { | |
val f: PartialFunction[Char, Int] = {case '+' => 1; case '-' => -1} | |
println("-"*5 + " partial function match " + "-"*5) | |
println("f('-') : " + f('-')) | |
println("f.isDefinedAt('0') : " + f.isDefinedAt('0')) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment