Skip to content

Commit d104f7c

Browse files
committed
pattern matching from chapter 18
1 parent 98b5d26 commit d104f7c

10 files changed

Lines changed: 145 additions & 128 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package s4j.scala.chatper18
2+
3+
object BasicConstructorPatternExample extends App {
4+
5+
val hero = new SuperHero("Batman", "Bruce Wayne", List("Intellect", "Speed", "Agility", "Strength"))
6+
7+
hero match {
8+
case SuperHero(_, "Bruce Wayne", _) => println("I'm Batman!")
9+
case SuperHero(_, _, _) => println("I'm Batman!")
10+
case _ => println("I'm a civilian")
11+
}
12+
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package s4j.scala.chatper18
2+
3+
class Customer(val name: String, val address: String) {
4+
var yearsACustomer: Int = 0
5+
}
6+
7+
object Customer {
8+
def unapply(customer: Customer): Option[(String, String)] = Some((customer.name, customer.address))
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package s4j.scala.chatper18
2+
3+
case class Discount(value: Double)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package s4j.scala.chatper18
2+
3+
import s4j.scala.chatper18.Extractors.{DiscountExtractor, UrlExtractor, YearsOfCustom}
4+
5+
object ExtractorExample extends App {
6+
val today = ""
7+
8+
val customer = new Customer("Bob", "1 Church street")
9+
customer match {
10+
case Customer(name, address) => println(name + " " + address)
11+
}
12+
13+
customer.yearsACustomer = 3
14+
val discount = customer match {
15+
case YearsOfCustom(years) if years >= 5 => Discount(0.5)
16+
case YearsOfCustom(years) if years >= 2 => Discount(0.2)
17+
case YearsOfCustom(years) if years >= 1 => Discount(0.1)
18+
case _ if blackFriday(today) => Discount(0)
19+
case _ => Discount(0)
20+
}
21+
println(discount)
22+
23+
val discount2 = customer match {
24+
case DiscountExtractor(discount) => discount
25+
}
26+
println(discount2)
27+
28+
val url = "http://baddotrobot.com" match {
29+
case UrlExtractor(protocol, host) => println(protocol + " " + host)
30+
}
31+
32+
def blackFriday(x: String): Boolean = true
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package s4j.scala.chatper18
2+
3+
import java.net.{MalformedURLException, URL}
4+
5+
object Extractors {
6+
7+
object YearsOfCustom {
8+
def unapply(customer: Customer): Option[Int] = Some(customer.yearsACustomer)
9+
}
10+
11+
12+
object UrlExtractor {
13+
def unapply(string: String): Option[(String, String)] = {
14+
try {
15+
val url = new URL(string)
16+
Some((url.getProtocol, url.getHost))
17+
} catch {
18+
case _: MalformedURLException => None
19+
}
20+
}
21+
}
22+
23+
24+
object DiscountExtractor {
25+
def unapply(customer: Customer): Option[Discount] = {
26+
if (customer.yearsACustomer >= 5) Some(Discount(0.5))
27+
else if (customer.yearsACustomer >= 2) Some(Discount(0.2))
28+
else if (customer.yearsACustomer >= 1) Some(Discount(0.1))
29+
else None
30+
}
31+
}
32+
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package s4j.scala.chatper18
2+
3+
object HeroConstructorPatternExample extends App {
4+
5+
val bruce = new SuperHero("Batman", "Bruce Wayne", List("Intellect", "Speed", "Agility", "Strength"))
6+
val youngBruce = new Person("Bruce Wayne")
7+
val jane = new Person("Jane Doe")
8+
9+
def superPowersFor(person: Person) = {
10+
person match {
11+
case SuperHero(_, name, powers) => powers
12+
case _ => List()
13+
}
14+
}
15+
16+
// Example of a constructor pattern
17+
18+
// What super powers does a person have, if they are in fact a super hero who's alter ego is Bruce Wayne?
19+
val person = bruce
20+
println("Bruce has the following powers " + superPowersFor(person))
21+
println("Young Bruce has the following powers " + superPowersFor(youngBruce))
22+
println("Jane has the following powers " + superPowersFor(jane))
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package s4j.scala.chatper18
2+
3+
object HeroTypePatternExample extends App {
4+
5+
val batman = new SuperHero("Batman", "Bruce Wayne", List("Intellect", "Speed", "Agility", "Strength"))
6+
val youngBruce = new Person("Bruce Wayne")
7+
val jane = new Person("Jane Doe")
8+
9+
def nameFor(person: Person) = {
10+
person match {
11+
case hero: SuperHero => hero.alterEgo
12+
case person: Person => person.name
13+
}
14+
}
15+
16+
// Example of type check pattern
17+
18+
// What's an super hero's alter ego?
19+
println("Batman's Alter ego is " + nameFor(batman))
20+
println("Young Bruce's Alter ego is " + nameFor(youngBruce))
21+
println("Jane's Alter ego is " + nameFor(jane))
22+
}
Lines changed: 3 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,22 @@
11
package s4j.scala.chatper18
22

3-
import java.net.{MalformedURLException, URL}
4-
53
object PatternMatchingExamples extends App {
64

75
"value" match {
86
case "z" => println("z")
9-
case a => println(a)
10-
case _ => ???
7+
case a => println(a)
8+
case _ => ???
119
}
1210

1311
// constructor pattern
1412
Foo("bob", 7) match {
1513
case Foo(x, 9) => println(x + " 09")
1614
case Foo(x, _) => println(x)
17-
case _ => ???
15+
case _ => ???
1816
}
1917

2018
case class Foo(string: String, int: Int) {
2119
def this(string: String) = this(string, 0)
2220
}
2321

2422
}
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)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package s4j.scala.chatper18
2+
3+
class Person(val name: String)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package s4j.scala.chatper18
2+
3+
case class SuperHero(heroName: String, alterEgo: String, powers: List[String]) extends Person(alterEgo)

0 commit comments

Comments
 (0)