Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
Loading in …5
×

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

536 views

Published on

- パラメータ化された型
- 存在型
- 数値とモノイド
- 多相と型クラス
cf. part1: https://www.slideshare.net/opttechnologies/scala-handson-textbook-83475085

Published in: Software
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

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

1. 1. Java Scala
2. 2. C++ swap Scala length sum
3. 3. length
4. 4. length
5. 5. def length(list: List[Double]): Double = list match { case Nil => 0 case _ :: tail => 1 + length(tail) }
6. 6. val list = List(1.0, 3.14, 2.71) length(list) // 3
7. 7. val list = List(42, 99, 666)
8. 8. length
9. 9. error: type mismatch; found : List[Int] required: List[Double] length(list)
10. 10. List[Double] List[Int]
11. 11. length List[Double] => Double
12. 12. length
13. 13. Double Int String Any
14. 14. def length[A](list: List[A]): Double = list match { case Nil => 0 case _ :: tail => 1 + length(tail) }
15. 15. length [A]
16. 16. 1
17. 17. new
18. 18. def func[A, B, C](arga: A, argb: B): C = ...
19. 19. length length [A]
20. 20. length
21. 21. length
22. 22. val listDouble = List(1.0, 3.14, 2.71) length[Double](listDouble) // 3 val listInt = List(1, 2, 3, 4, 5) length[Int](listInt) // 5 val listString = List("hoge", "fuga", "piyo") length[String](listString) // 3
23. 23. Scala
24. 24. val listDouble = List(1.0, 3.14, 2.71) length(listDouble) // 3 val listInt = List(1, 2, 3, 4, 5) length(listInt) // 5
25. 25. length
26. 26. length
27. 27. def length[A](list: List[A]): Double = list match { case Nil => 0 case _ :: tail => 1 + length(tail) }
28. 28. A A
29. 29. 2 2
30. 30. def twoList[A, B, C](list1: List[A], list2: List[B], func: List[? (func(list1), func(list2))
31. 31. 3
32. 32. List[A] List[B]
33. 33. length
34. 34. def length(list: List[_]): Double = list match { case Nil => 0 case _ :: tail => 1 + length(tail) }
35. 35. length List [_]
36. 36. A A
37. 37. def func(list: List[_ <: AnyVal]) = ...
38. 38. A <: B A B
39. 39. twoList(List(1,2,3,4,5), List("hoge","fuga","piyo"), length) // (
40. 40. 8-1
41. 41. sum
42. 42. sum
43. 43. def sum(list: List[Double]): Double = list match { case Nil => 0 case head :: tail => head + sum(tail) }
44. 44. Double List
45. 45. val list = List(1.0, 3.14, 2.71) sum(list) // 6.85
46. 46. List(42, 99, 666)
47. 47. val list = List(42, 99, 666) sum(list) // error: type mismatch; // found : List[Int] // required: List[Double] // sum(list)
48. 48. length
49. 49. Double Int
50. 50. length sum Int Double
51. 51. def sum[A](list: List[A]): A = list match { case Nil => 0 case head :: tail => head + sum(tail) }
52. 52. ……
53. 53. type mismatch; found : Int(0) required: A case Nil => 0 ^ type mismatch; found : List[A] required: List[String] case head :: tail => head + sum(tail) ^
54. 54. sum Scala
55. 55. def sum[A](list: List[A]): A =
56. 56. A A A
57. 57. case Nil => 0
58. 58. A Int
59. 59. Scala
60. 60. case head :: tail => head + sum(tail)
61. 61. + A +
62. 62. sum sum
63. 63. 2 0
64. 64. 2
65. 65. <:
66. 66. trait Addable { def +(other: Addable): Addable } def add[T <: Addable](a: T, b: T) = a + b
67. 67. T + +
68. 68. Addable
69. 69. Scala Int Double
70. 70. Scala Numeric
71. 71. Haskell Scala
72. 72. implicit
73. 73. Scala
74. 74. def sum[A](list: List[A])(implicit ev: Numeric[A]): A =
75. 75. 2 implicit [A]
76. 76. A ev evidence
77. 77. Ruby
78. 78. Numeric
79. 79. trait Numeric[T] extends Ordering[T] { def plus(x: T, y: T): T def minus(x: T, y: T): T def times(x: T, y: T): T def negate(x: T): T def fromInt(x: Int): T def toInt(x: T): Int def toLong(x: T): Long def toFloat(x: T): Float def toDouble(x: T): Double def zero = fromInt(0) def one = fromInt(1) def abs(x: T): T = if (lt(x, zero)) negate(x) else x
80. 80. sum
81. 81. 0
82. 82. plus zero Numeric
83. 83. sum Numeric Scala Numeric
84. 84. Numeric
85. 85. def sum[A](list: List[A])(implicit ev: Numeric[A]): A = list match { case Nil => ev.zero case head :: tail => ev.plus(head, sum(tail)) }
86. 86. sum
87. 87. ev
88. 88. ev
89. 89. val listDouble = List(1.0, 3.14, 2.71) sum(listDouble) // 6.85 val listInt = List(1, 2, 3, 4, 5) sum(listInt) // 15
90. 90. sum
91. 91. 9-1
92. 92. 9-2 sum
93. 93. def sum[A: Numeric](list: List[A]): A = list match { case Nil => implicitly[Numeric[A]].zero case head :: tail => implicitly[Numeric[A]].plus(head, sum(ta }
94. 94. [A: Numeric] context bound
95. 95. implicitly
96. 96. def implicitly[T](implicit e: T) = e
97. 97. 9-3 Scala