-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPatternmatchDemo12.scala
31 lines (26 loc) · 1.06 KB
/
PatternmatchDemo12.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package ScalaBasic
// https://www.youtube.com/watch?v=uT1gcG_weYM&list=PLmOn9nNkQxJEqCNXBu5ozT_26xwvUbHyE&index=158
object PatternmatchDemo12 extends App {
// example 1
/**
* Pattern match with "Infix notation"
*
* 1) element1 :: element2 -> "Infix notation"
* 2) first match first element, and second match second element, rest match rest of the elements
* 3) only match "format", variables name can be variable ( e.g. : first, second, rest...)
*/
List(1,3,5,9) match {
case first :: second :: rest => println(first.toString + ", " + second.toString + ", rest.length = " + rest.length)
case _ => println("pattern match failed")
}
// example 2
List(1,3) match {
case first :: second :: rest => println(first.toString + ", " + second.toString + ", rest.length = " + rest.length)
case _ => println("pattern match failed")
}
// example 3
List(1) match {
case first :: second :: rest => println(first.toString + ", " + second.toString + ", rest.length = " + rest.length)
case _ => println("pattern match failed")
}
}