Skip to content

Commit a545f94

Browse files
committed
chapter 16
1 parent 3655501 commit a545f94

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package s4j.scala.chapter16
2+
3+
object ArrayExample extends App {
4+
5+
val numerals = Array("I", "II", "III", "IV", "V", "VI", "VII")
6+
7+
println(numerals(5))
8+
9+
for (i <- 0 to numerals.length - 1)
10+
println(i + " = " + numerals(i))
11+
12+
// could have written this
13+
for (i <- 0 to numerals.length - 1)
14+
println(i + " = " + numerals.apply(i))
15+
16+
// more functional version
17+
for ((value, index) <- numerals.zipWithIndex)
18+
println(index + " : " + value)
19+
20+
numerals(1) = "?"
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package s4j.scala.chapter16
2+
3+
class Directory {
4+
private val numbers = scala.collection.mutable.Map(
5+
"Athos" -> "7781 456782",
6+
"Aramis" -> "7781 823422",
7+
"Porthos" -> "1471 342383",
8+
"D`Artagnan" -> "7715 632982"
9+
)
10+
11+
def apply(name: String) = {
12+
numbers.get(name)
13+
}
14+
15+
def update(name: String, number: String) = {
16+
numbers.update(name, number)
17+
}
18+
19+
def update(areaCode: Int, newAreaCode: String) = {
20+
numbers.foreach(entry => {
21+
if (entry._2.startsWith(areaCode.toString))
22+
numbers(entry._1) = entry._2.replace(areaCode.toString, newAreaCode)
23+
})
24+
}
25+
26+
def update(areaCode: Int, anotherArgument: String, newAreaCode: String) = ???
27+
28+
override def toString = numbers.toString()
29+
}
30+
31+
object Directory extends App {
32+
33+
val yellowPages = new Directory()
34+
println(yellowPages)
35+
yellowPages(7781) = "7555"
36+
yellowPages(7998, "another argument") = "???"
37+
println(yellowPages)
38+
39+
}

0 commit comments

Comments
 (0)