Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. /*
  2. A pattern match includes a sequence of alternatives, each starting with the keyword case.
  3. Each alternative includes a pattern and one or more expressions, which will be evaluated
  4. if the pattern matches. An arrow symbol => separates the pattern from the expressions.
  5. */
  6.  
  7. def matchTest(x: Int): String = x match {
  8. case 1 => "one"
  9. case 2 => "two"
  10. case _ => "many"
  11. }
  12.  
  13. matchTest(1)
  14. matchTest(2)
  15. matchTest(3)
  16.  
  17. def matchTest2(x: Any): Any = x match {
  18. case 1 => "one"
  19. case "two" => 2
  20. case y: Int => "scala.Int"
  21. case _ => "many"
  22. }
  23.  
  24. matchTest2(1)
  25. matchTest2("two")
  26. matchTest2(3)
  27.  
  28.  
  29.  
  30.  
  31.  
  32. sealed class Account
  33. case class Paypal(email:String) extends Account
  34. case class Bitcoin(key:String) extends Account
  35. case class User(name:String, account:Account)
  36.  
  37. def whatis1(obj: Any) = {
  38. if (obj.isInstanceOf[User]) {
  39. val user = obj.asInstanceOf[User]
  40. if (user.account.isInstanceOf[Paypal]) {
  41. val paypal = user.account.asInstanceOf[Paypal]
  42. "Paypal for email" + paypal.email
  43. }
  44. else if (user.account.isInstanceOf[Bitcoin]) {
  45. val bitcoin = user.account.asInstanceOf[Bitcoin]
  46. "Bitcoin with key " + bitcoin.key
  47. }
  48. else {
  49. "Unknown account type"
  50. }
  51. } else {
  52. "Unknown object type"
  53. }
  54.  
  55. }
  56.  
  57. def whatis2(obj:Any) = {
  58. obj match {
  59. case User(name, Paypal(email)) =>
  60. "Paypal for meail " + email
  61. case User(name, Bitcoin(email)) =>
  62. "Bitcoin for meail " + email
  63. case User(name, _) =>
  64. "Unknown account type"
  65. case _ => "Unknown object type"
  66. }
  67. }
  68.  
  69. whatis1(User("bilbo", Paypal("bilbo@shire.com")))
  70. whatis2(User("bilbo", Paypal("bilbo@shire.com")))
  71.  
  72. case class WellsFargo(name:String) extends Account
  73. whatis2(User("bilbo", WellsFargo("bilbo@shire.com")))
  74. whatis2(WellsFargo("johnbush"))
  75.  
  76. def whatis3(obj: Any) = {
  77. if (obj.isInstanceOf[User]) {
  78. val user = obj.asInstanceOf[User]
  79. if (!user.name.isEmpty) {
  80. if (user.account.isInstanceOf[Paypal]) {
  81. val paypal = user.account.asInstanceOf[Paypal]
  82. if (paypal.equals("bilbo@shire.com"))
  83. "Bilbo's got Paypal"
  84. }
  85. }
  86. }
  87. }
  88.  
  89. def whatis4(obj:Any) = {
  90. obj match {
  91. case User(name, Paypal("bilbo@shire.com"))
  92. if !name.isEmpty => "Bilbo's got Paypal"
  93. }
  94. }
  95.  
  96. def getTuple: (User, Account) = (User("bilbo", Paypal("bilbo@shire.com")), Paypal("bilbo@shire.com"))
  97. val u = getTuple._1
  98. val a = getTuple._2
  99. val (user, account) = getTuple
  100.  
  101.  
  102. case class Dog(name:String)
  103. case class Person(first:String, last:String)
  104.  
  105. def echoWhatYouGaveMe(x: Any): String = x match {
  106.  
  107. // constant patterns
  108. case 0 => "zero"
  109. case true => "true"
  110. case "hello" => "you said 'hello'"
  111. case Nil => "an empty List"
  112.  
  113. // sequence patterns
  114. case List(0, _, _) => "a three-element list with 0 as the first element"
  115. case List(1, _*) => "a list beginning with 1, having any number of elements"
  116. case Vector(1, _*) => "a vector starting with 1, having any number of elements"
  117.  
  118. // tuples
  119. case (a, b) => s"got $a and $b"
  120. case (a, b, c) => s"got $a, $b, and $c"
  121.  
  122. // constructor patterns
  123. case Person(first, "Alexander") => s"found an Alexander, first name = $first"
  124. case Dog("Suka") => "found a dog named Suka"
  125.  
  126. // typed patterns
  127. case s: String => s"you gave me this string: $s"
  128. case i: Int => s"thanks for the int: $i"
  129. case f: Float => s"thanks for the float: $f"
  130. case a: Array[Int] => s"an array of int: ${a.mkString(",")}"
  131. case as: Array[String] => s"an array of strings: ${as.mkString(",")}"
  132. case d: Dog => s"dog: ${d.name}"
  133. case list: List[_] => s"thanks for the List: $list"
  134. case m: Map[_, _] => m.toString
  135.  
  136. // the default wildcard pattern
  137. case _ => "Unknown"
  138. }
  139.  
  140. // trigger the constant patterns
  141. echoWhatYouGaveMe(0)
  142. echoWhatYouGaveMe(true)
  143. echoWhatYouGaveMe("hello")
  144. echoWhatYouGaveMe(List())
  145.  
  146. // trigger the sequence patterns
  147. echoWhatYouGaveMe(List(0,1,2))
  148. echoWhatYouGaveMe(List(1,2))
  149. echoWhatYouGaveMe(List(1,2,3))
  150. echoWhatYouGaveMe(Vector(1,2,3))
  151.  
  152. // trigger the tuple patterns
  153. echoWhatYouGaveMe((1,2)) // two element tuple
  154. echoWhatYouGaveMe((1,2,3)) // three element tuple
  155.  
  156. // trigger the constructor patterns
  157. echoWhatYouGaveMe(Person("Melissa", "Alexander"))
  158. echoWhatYouGaveMe(Dog("Suka"))
  159.  
  160. // trigger the typed patterns
  161. echoWhatYouGaveMe("Hello, world")
  162. echoWhatYouGaveMe(42)
  163. echoWhatYouGaveMe(42F)
  164. echoWhatYouGaveMe(Array(1,2,3))
  165. echoWhatYouGaveMe(Array("coffee", "apple pie"))
  166. echoWhatYouGaveMe(Dog("Fido"))
  167. echoWhatYouGaveMe(List("apple", "banana"))
  168. echoWhatYouGaveMe(Map(1->"Al", 2->"Alexander"))
  169.  
  170. // trigger the wildcard pattern
  171. echoWhatYouGaveMe("33d")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement