Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- A pattern match includes a sequence of alternatives, each starting with the keyword case.
- Each alternative includes a pattern and one or more expressions, which will be evaluated
- if the pattern matches. An arrow symbol => separates the pattern from the expressions.
- */
- def matchTest(x: Int): String = x match {
- case 1 => "one"
- case 2 => "two"
- case _ => "many"
- }
- matchTest(1)
- matchTest(2)
- matchTest(3)
- def matchTest2(x: Any): Any = x match {
- case 1 => "one"
- case "two" => 2
- case y: Int => "scala.Int"
- case _ => "many"
- }
- matchTest2(1)
- matchTest2("two")
- matchTest2(3)
- sealed class Account
- case class Paypal(email:String) extends Account
- case class Bitcoin(key:String) extends Account
- case class User(name:String, account:Account)
- def whatis1(obj: Any) = {
- if (obj.isInstanceOf[User]) {
- val user = obj.asInstanceOf[User]
- if (user.account.isInstanceOf[Paypal]) {
- val paypal = user.account.asInstanceOf[Paypal]
- "Paypal for email" + paypal.email
- }
- else if (user.account.isInstanceOf[Bitcoin]) {
- val bitcoin = user.account.asInstanceOf[Bitcoin]
- "Bitcoin with key " + bitcoin.key
- }
- else {
- "Unknown account type"
- }
- } else {
- "Unknown object type"
- }
- }
- def whatis2(obj:Any) = {
- obj match {
- case User(name, Paypal(email)) =>
- "Paypal for meail " + email
- case User(name, Bitcoin(email)) =>
- "Bitcoin for meail " + email
- case User(name, _) =>
- "Unknown account type"
- case _ => "Unknown object type"
- }
- }
- whatis1(User("bilbo", Paypal("bilbo@shire.com")))
- whatis2(User("bilbo", Paypal("bilbo@shire.com")))
- case class WellsFargo(name:String) extends Account
- whatis2(User("bilbo", WellsFargo("bilbo@shire.com")))
- whatis2(WellsFargo("johnbush"))
- def whatis3(obj: Any) = {
- if (obj.isInstanceOf[User]) {
- val user = obj.asInstanceOf[User]
- if (!user.name.isEmpty) {
- if (user.account.isInstanceOf[Paypal]) {
- val paypal = user.account.asInstanceOf[Paypal]
- if (paypal.equals("bilbo@shire.com"))
- "Bilbo's got Paypal"
- }
- }
- }
- }
- def whatis4(obj:Any) = {
- obj match {
- case User(name, Paypal("bilbo@shire.com"))
- if !name.isEmpty => "Bilbo's got Paypal"
- }
- }
- def getTuple: (User, Account) = (User("bilbo", Paypal("bilbo@shire.com")), Paypal("bilbo@shire.com"))
- val u = getTuple._1
- val a = getTuple._2
- val (user, account) = getTuple
- case class Dog(name:String)
- case class Person(first:String, last:String)
- def echoWhatYouGaveMe(x: Any): String = x match {
- // constant patterns
- case 0 => "zero"
- case true => "true"
- case "hello" => "you said 'hello'"
- case Nil => "an empty List"
- // sequence patterns
- case List(0, _, _) => "a three-element list with 0 as the first element"
- case List(1, _*) => "a list beginning with 1, having any number of elements"
- case Vector(1, _*) => "a vector starting with 1, having any number of elements"
- // tuples
- case (a, b) => s"got $a and $b"
- case (a, b, c) => s"got $a, $b, and $c"
- // constructor patterns
- case Person(first, "Alexander") => s"found an Alexander, first name = $first"
- case Dog("Suka") => "found a dog named Suka"
- // typed patterns
- case s: String => s"you gave me this string: $s"
- case i: Int => s"thanks for the int: $i"
- case f: Float => s"thanks for the float: $f"
- case a: Array[Int] => s"an array of int: ${a.mkString(",")}"
- case as: Array[String] => s"an array of strings: ${as.mkString(",")}"
- case d: Dog => s"dog: ${d.name}"
- case list: List[_] => s"thanks for the List: $list"
- case m: Map[_, _] => m.toString
- // the default wildcard pattern
- case _ => "Unknown"
- }
- // trigger the constant patterns
- echoWhatYouGaveMe(0)
- echoWhatYouGaveMe(true)
- echoWhatYouGaveMe("hello")
- echoWhatYouGaveMe(List())
- // trigger the sequence patterns
- echoWhatYouGaveMe(List(0,1,2))
- echoWhatYouGaveMe(List(1,2))
- echoWhatYouGaveMe(List(1,2,3))
- echoWhatYouGaveMe(Vector(1,2,3))
- // trigger the tuple patterns
- echoWhatYouGaveMe((1,2)) // two element tuple
- echoWhatYouGaveMe((1,2,3)) // three element tuple
- // trigger the constructor patterns
- echoWhatYouGaveMe(Person("Melissa", "Alexander"))
- echoWhatYouGaveMe(Dog("Suka"))
- // trigger the typed patterns
- echoWhatYouGaveMe("Hello, world")
- echoWhatYouGaveMe(42)
- echoWhatYouGaveMe(42F)
- echoWhatYouGaveMe(Array(1,2,3))
- echoWhatYouGaveMe(Array("coffee", "apple pie"))
- echoWhatYouGaveMe(Dog("Fido"))
- echoWhatYouGaveMe(List("apple", "banana"))
- echoWhatYouGaveMe(Map(1->"Al", 2->"Alexander"))
- // trigger the wildcard pattern
- echoWhatYouGaveMe("33d")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement