|
1 | 1 | package s4j.scala.chatper18 |
2 | 2 |
|
3 | | -import java.net.{MalformedURLException, URL} |
4 | | - |
5 | 3 | object PatternMatchingExamples extends App { |
6 | 4 |
|
7 | 5 | "value" match { |
8 | 6 | case "z" => println("z") |
9 | | - case a => println(a) |
10 | | - case _ => ??? |
| 7 | + case a => println(a) |
| 8 | + case _ => ??? |
11 | 9 | } |
12 | 10 |
|
13 | 11 | // constructor pattern |
14 | 12 | Foo("bob", 7) match { |
15 | 13 | case Foo(x, 9) => println(x + " 09") |
16 | 14 | case Foo(x, _) => println(x) |
17 | | - case _ => ??? |
| 15 | + case _ => ??? |
18 | 16 | } |
19 | 17 |
|
20 | 18 | case class Foo(string: String, int: Int) { |
21 | 19 | def this(string: String) = this(string, 0) |
22 | 20 | } |
23 | 21 |
|
24 | 22 | } |
25 | | - |
26 | | -class Person(val name: String) |
27 | | - |
28 | | -case class SuperHero(heroName: String, alterEgo: String, powers: List[String]) extends Person(alterEgo) |
29 | | - |
30 | | -object BasicConstructorPatternExample extends App { |
31 | | - |
32 | | - val hero = new SuperHero("Batman", "Bruce Wayne", List("Intellect", "Speed", "Agility", "Strength")) |
33 | | - |
34 | | - hero match { |
35 | | - case SuperHero(_, "Bruce Wayne", _) => println("I'm Batman!") |
36 | | - case SuperHero(_, _, _) => println("I'm Batman!") |
37 | | - case _ => println("I'm a civilian") |
38 | | - } |
39 | | - |
40 | | -} |
41 | | - |
42 | | -object HeroConstructorPatternExample extends App { |
43 | | - |
44 | | - val bruce = new SuperHero("Batman", "Bruce Wayne", List("Intellect", "Speed", "Agility", "Strength")) |
45 | | - val youngBruce = new Person("Bruce Wayne") |
46 | | - val jane = new Person("Jane Doe") |
47 | | - |
48 | | - def superPowersFor(person: Person) = { |
49 | | - person match { |
50 | | - case SuperHero(_, name, powers) => powers |
51 | | - case _ => List() |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - // Example of a constructor pattern |
56 | | - |
57 | | - // What super powers does a person have, if they are in fact a super hero who's alter ego is Bruce Wayne? |
58 | | - val person = bruce |
59 | | - println("Bruce has the following powers " + superPowersFor(person)) |
60 | | - println("Young Bruce has the following powers " + superPowersFor(youngBruce)) |
61 | | - println("Jane has the following powers " + superPowersFor(jane)) |
62 | | -} |
63 | | - |
64 | | -object HeroTypePatternExample extends App { |
65 | | - |
66 | | - val batman = new SuperHero("Batman", "Bruce Wayne", List("Intellect", "Speed", "Agility", "Strength")) |
67 | | - val youngBruce = new Person("Bruce Wayne") |
68 | | - val jane = new Person("Jane Doe") |
69 | | - |
70 | | - def nameFor(person: Person) = { |
71 | | - person match { |
72 | | - case hero: SuperHero => hero.alterEgo |
73 | | - case person: Person => person.name |
74 | | - } |
75 | | - } |
76 | | - |
77 | | - // Example of type check pattern |
78 | | - |
79 | | - // What's an super hero's alter ego? |
80 | | - println("Batman's Alter ego is " + nameFor(batman)) |
81 | | - println("Young Bruce's Alter ego is " + nameFor(youngBruce)) |
82 | | - println("Jane's Alter ego is " + nameFor(jane)) |
83 | | -} |
84 | | - |
85 | | -object ExtractorExample extends App { |
86 | | - val today = "" |
87 | | - |
88 | | - val customer = new Customer("Bob", "1 Church street") |
89 | | - customer match { |
90 | | - case Customer(name, address) => println(name + " " + address) |
91 | | - } |
92 | | - |
93 | | - customer.yearsACustomer = 3 |
94 | | - val discount = customer match { |
95 | | - case YearsOfCustom(years) if years >= 5 => Discount(0.5) |
96 | | - case YearsOfCustom(years) if years >= 2 => Discount(0.2) |
97 | | - case YearsOfCustom(years) if years >= 1 => Discount(0.1) |
98 | | - case _ if blackFriday(today) => Discount(0) |
99 | | - case _ => Discount(0) |
100 | | - } |
101 | | - println(discount) |
102 | | - |
103 | | - val discount2 = customer match { |
104 | | - case DiscountExtractor(d) => d |
105 | | - } |
106 | | - println(discount2) |
107 | | - |
108 | | - val url = "http://baddotrobot.com" match { |
109 | | - case UrlExtractor(protocol, host) => println(protocol + " " + host) |
110 | | - } |
111 | | - |
112 | | - def blackFriday(x: String): Boolean = true |
113 | | -} |
114 | | - |
115 | | -class Customer(val name: String, val address: String) { |
116 | | - var yearsACustomer: Int = 0 |
117 | | -} |
118 | | - |
119 | | -object Customer { |
120 | | - def unapply(customer: Customer): Option[(String, String)] = Some((customer.name, customer.address)) |
121 | | -} |
122 | | - |
123 | | -object YearsOfCustom { |
124 | | - def unapply(customer: Customer): Option[Int] = Some(customer.yearsACustomer) |
125 | | -} |
126 | | - |
127 | | -object DiscountExtractor { |
128 | | - def unapply(customer: Customer): Option[Discount] = { |
129 | | - if (customer.yearsACustomer >= 5) Some(Discount(0.5)) |
130 | | - else if (customer.yearsACustomer >= 2) Some(Discount(0.2)) |
131 | | - else if (customer.yearsACustomer >= 1) Some(Discount(0.1)) |
132 | | - else None |
133 | | - } |
134 | | -} |
135 | | - |
136 | | -object UrlExtractor { |
137 | | - def unapply(string: String): Option[(String, String)] = { |
138 | | - try { |
139 | | - val url = new URL(string) |
140 | | - Some((url.getProtocol, url.getHost)) |
141 | | - } catch { |
142 | | - case _: MalformedURLException => None |
143 | | - } |
144 | | - } |
145 | | -} |
146 | | - |
147 | | -case class Discount(value: Double) |
0 commit comments