Successfully reported this slideshow.
Upcoming SlideShare
×

# 学生向けScalaハンズオンテキスト

1,444 views

Published on

- プログラミング言語「Scala」
- Scalaの基本文法
- 平均値を表示するCLIツールを作ってみよう

Published in: Software
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### 学生向けScalaハンズオンテキスト

1. 1. Scala
2. 2. Scala
3. 3. Scala JVM
4. 4. JVM
5. 5. OS
6. 6. VM OS VM 1
7. 7. Scala
8. 8. Scala
9. 9. Scala
10. 10. // 1 /* * */ println("Hello, world!") //
11. 11. 42 // Int 3.14 // Double "hogehoge" // String true // Boolean () // Unit
12. 12. Array(1, 2, 3) // List(1, 2, 3) // Map("a" -> 1, "b" -> 2) // Set(1, 2, 3) //
13. 13. 1 :: 2 :: 3 :: Nil // :: Nil // List(1, 2, 3)
14. 14. (42, "string", 3.14) // s"\$value \${value2}" // \$ \${}
15. 15. val i = 42 // val // i = 99 // val val j: Int = 100 // // k: String = 100 // var x = 42 // x = 99 //
16. 16. Scala if for match while
17. 17. // if (cond) cond Boolean if (cond) { ... } else { ... }
18. 18. for (i <- seq) { ... }
19. 19. // Scala if for val ret = if (false) { "aaa" } else if (true) { "bbb" } else { "ccc" } ret // => "bbb"
20. 20. val list = 1 :: 2 :: 3 :: Nil val ret = for (i <- list) { i * 2 } ret //=> Unit // for // list.foreach { i => ... } val list = for (i <- list) yield { i * 2 } list //=> List(2, 4, 6) // yield yield // list.map { i => ... }
21. 21. val num = 42 // match num match { case n if n > 40 => println("over 40.") case _ => println("less than or equal to 40.") // _ }
22. 22. val tuple = (42, "answer") val (i, s) = tuple i // 42 s // tuple // match tuple match { case (40, s) => println(s"40 is \$s") case (i, "universe") => println(s"\$i is universe") case (i, s) => println(s"\$i is \$s") }
23. 23. val tuple = (42, "hogefuga", 3.14) tuple match { case (40, "foobar", 2.718) => // case (Num, Str, Point) => // case (`num`, `str`, `p`) => // case (n: Int, s: String, p: Int) => // case tup @ (n, s, p) => // }
24. 24. val list = 1 :: 2 :: 3 :: Nil // head tail val head :: tail = list head // 1 tail // List(2, 3) list match { case 42 :: tail => // 42 case head :: Nil => // Nil 1 case e1 :: e2 :: _ => // 2 case Nil => // }
25. 25. // val tuple = (42, "answer", 1 :: 2 :: 3 :: Nil) tuple match { case (40, str, head :: Nil) => case (i, "answer", x :: xs) => case (_, _, list @ (head :: tail)) => // list List case _ => // }
26. 26. Scala def
27. 27. def func(x: Int, y: Int): Int = { x + y } // // 1 def func2(x: Int, y: Int) = x + y
28. 28. Scala
29. 29. // val fun = (x: Int, y: Int) => x + y // fun(1, 2) //=> 3
30. 30. =>
31. 31. // x y Int val fun: (Int, Int) => Int = (x, y) => x + y
32. 32. =>
33. 33. match => =>
34. 34. // => val fun: Int => String = (i: Int) => i match { case 42 => "correct" }
35. 35. val fun = (x: Int, y: Int) = x + y // def fortytwo(num: Int, f: (Int, Int) => Int) = f(num, 42) // fortytwo(99, fun) // 141 // fortytwo(2, (x, y) => x * y) // 84 // fortytwo x y
36. 36. map
37. 37. map map
38. 38. Scala List map
39. 39. val list = 1 :: 2 :: 3 :: Nil // 2 val fun = i: Int => i * 2 // map list.map(fun) //=> List(2, 4, 6) // List fun // list.map(i => i + 2) // List(3, 4, 5)
40. 40. CLI
41. 41. \$ scala cli.scala 74 76 81 89 92 87 79 85 mean: 82
42. 42. object Main { def main(args: Array[String]): Unit = { println("Hello, world!") } }
43. 43. cli.scala Hello, world! \$ scala cli.scala
44. 44. Scala
45. 45. sbt
46. 46. Scala sbt
47. 47. sbt \$ sbt
48. 48. compile run main
49. 49. sbt sbt target
50. 50. 1-1
51. 51. Scala main
52. 52. object Main { def main(args: Array[String]): Unit = { for (arg <- args) { println(arg) } } }
53. 53. args.scala
54. 54. \$ scala args.scala hoge fuga hoge fuga \$ scala args.scala 42 99 42 99
55. 55. 2-1
56. 56. Scala 2.11 scala.io.StdIn
57. 57. object Main { def main(args: Array[String]): Unit = { val input = scala.io.StdIn.readLine println(input) } }
58. 58. stdin.scala
59. 59. \$ scala stdin.scala
60. 60. \$ scala stdin.scala Hello, world! Hello, world!
61. 61. split
62. 62. object Main { def main(args: Array[String]): Unit = { val input = scala.io.StdIn.readLine.split(" ") for (word <- input) println(word) } }
63. 63. \$ scala stdin.scala hoge fuga piyo hoge fuga piyo
64. 64. 2-2
65. 65. Scala ……
66. 66. 42 + "99" // toString "4299"
67. 67. Ruby JavaScript
68. 68. Ruby irb(main):001:0> 42 + "99" TypeError: String can't be coerced into Fixnum from (irb):1:in `+' from (irb):1
69. 69. JavaScript > 42 + '99' '4299' > '7' * 9 63 > 42 * 'hoge' NaN
70. 70. Scala NaN
71. 71. Scala implicit
72. 72. toInt toDouble
73. 73. 42 + "99".toInt // 141 3 * 3 * "3.14".toDouble // 28.26
74. 74. 2
75. 75. object Main { def main(args: Array[String]): Unit = { for (arg <- args) { println(arg.toInt * 2) } } }
76. 76. number.scala \$ scala number.scala 42 99 1 84 198 2
77. 77. 3-1 "hoge" toInt toDouble
78. 78. Array[String] -> List[Double]
79. 79. Array Array List toDouble
80. 80. toList Array toList List
81. 81. args.toList
82. 82. map map
83. 83. map Double
84. 84. args.map(_.toDouble)
85. 85. Array[String] List[Double] args.map(_.toDouble).toList
86. 86. 4-1 Array[String] List[Double]
87. 87. Array List val arr = Array("1", "2", "3") val list = List(1.0, 2.0, 3.0)
88. 88. CLI /
89. 89. sum
90. 90. sum Scala sum
91. 91. sum
92. 92. 0
93. 93. Scala Nil Nil sum(Nil) 0
94. 94. 1 2 3 …… n n
95. 95. n n n-1 n = 1 n-1
96. 96. n n 1..n 1..n 1..n-1 n
97. 97. 1 + 2 + ... + n - 2 + n - 1 + n = (1 + 2 + ... n - 2 + n - 1) + n
98. 98. 3 :: 2 :: 7 :: 5 :: Nil 1 4 17 1 3 12 4 5 17
99. 99. n 1
100. 100. n - 1 = 0 0 n - 1 > 0
101. 101. 0 0
102. 102. sum
103. 103. 0 n n 1..n-1
104. 104. Scala
105. 105. sum( 3 :: 2 :: 7 :: 5 :: Nil ) 3 + sum( 2 :: 7 :: 5 :: Nil ) 3 + 2 + sum( 7 :: 5 :: Nil) 3 + 2 + 7 + sum( 5 :: Nil) 3 + 2 + 7 + 5 + sum( Nil ) 3 + 2 + 7 + 5 + 0 3 + 2 + 7 + 5 3 + 2 + 12 3 + 14 17
106. 106. Scala
107. 107. Scala
108. 108. def sum(list: List[Double]): Double = ???
109. 109. List[Double] Double
110. 110. if match
111. 111. def sum(list: List[Double]): Double = list match { case Nil => ??? case head :: tail => ??? }
112. 112. 0
113. 113. def sum(list: List[Double]): Double = list match { case Nil => 0 case head :: tail => ??? }
114. 114. def sum(list: List[Double]): Double = list match { case Nil => 0 case head :: tail => head + sum(tail) }
115. 115. 5-1
116. 116. 1 :: 2 :: 3 :: Nil match { case a :: b :: rest => println(a) // 1 println(b) // 2 println(rest) // List(3) }
117. 117. 1
118. 118. length
119. 119. sum length
120. 120. sum 0 length
121. 121. sum 0
122. 122. n n n-1
123. 123. 1 1 n-1 1 n
124. 124. n n-1
125. 125. sum 3 :: 2 :: 7 :: 5 :: Nil
126. 126. length( 3 :: 2 :: 7 :: 5 :: Nil ) 1 + length( 2 :: 7 :: 5 :: Nil ) 1 + 1 + length( 7 :: 5 :: Nil ) 1 + 1 + 1 + length( 5 :: Nil ) 1 + 1 + 1 + 1 + length( Nil ) 1 + 1 + 1 + 1 + 0 1 + 1 + 1 + 1 1 + 1 + 2 1 + 3 4
127. 127. Scala
128. 128. def length(list: List[Double]): Double = list match { case Nil => 0 case _ :: tail => 1 + length(tail) }
129. 129. _ Scala
130. 130. _
131. 131. 6-1 _
132. 132. _
133. 133. mean
134. 134. /
135. 135. def mean(list: List[Double]): Double = sum(list) / length(list)
136. 136. CLI
137. 137. CLI
138. 138. object Main { def sum(list: List[Double]): Double = list match { case Nil => 0 case head :: tail => head + sum(tail) } def length(list: List[Double]): Double = list match { case Nil => 0 case _ :: tail => 1 + length(tail) } def mean(list: List[Double]): Double = sum(list) / length(list) def main(args: Array[String]): Unit = {
139. 139. \$ scala cli.scala mean: 82.875
140. 140. 2
141. 141. def sum(list: List[Double]): Double = list match { case Nil => 0 case head :: tail => head + sum(tail) } def length(list: List[Double]): Double = list match { case Nil => 0 case _ :: tail => 1 + length(tail) }
142. 142. def common(list: List[Double]): Double = list match { case Nil => 0 case head :: tail => ??? + common(tail) }
143. 143. ???
144. 144. def common(list: List[Double], f: Double => Double): Double = list match { case Nil => 0 case head :: tail => f(head) + common(tail, f) }
145. 145. 1
146. 146. sum length
147. 147. def sum(list: List[Double]): Double = common(list, head => head) def length(list: List[Double]): Double = common(list, _ => 1)
150. 150. _ => 1 1 1
151. 151. 7-1 _ => 1 head => 1 _
152. 152. match _
153. 153. 7-2
154. 154. def test(fun: Double => Double) = fun(3.14) test(_ => 1) // 1.0
155. 155. 1
156. 156. val fun = _: Double => 1 test(fun) //
157. 157. :
158. 158. 7-3 identity
159. 159. fold
160. 160. fold
161. 161. common
162. 162. fold
163. 163. fold sum length map ﬁlter
164. 164. def foldr[A, B](f: (A, B) => B, ini: B, list: List[A]): B = list match { case Nil => ini case head :: tail => f(head, foldr(f, ini, tail)) }
165. 165. Double length sum foldr
166. 166. def length(list: List[Double]): Double = foldr[Double, Double]((_, i) => i + 1, 0, list) def sum(list: List[Double]): Double = foldr[Double, Double]((a, i) => a + i, 0, list)
167. 167. 7-4 foldr
168. 168. max min reverse ﬁlter map