Successfully reported this slideshow.
Upcoming SlideShare
×

of

1

Share

# Scala kansai summit-2016

Scala 関西 Summit 2016 のハンズオンで使ったスライド

See all

See all

### Scala kansai summit-2016

1. 1. revival Scala Summit 2016
2. 2.
3. 3. Scala
4. 4. Scala ?
5. 5. ?
6. 6. •Scala • • Scala ?
7. 7. • • • •
8. 8. • • • •
9. 9. f(x) = x + 1 Scala • = ( ) • Which is better? def f(x: Int) {x + 1} def f(x: Int) = x + 1 def f(x: Int) = return x + 1 ※ : 2
10. 10. ( ) • (mutable) • •
11. 11. 2
12. 12. ? int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } return sum;
13. 13. i = i + 1; ?
14. 14. https://goo.gl/iKyEKs
15. 15. • 1 n • int f(int n) { int total = 0; for (int i = 1; i <= n; i++) { total += i; } return total; }
16. 16. f(1) = 1 f(2) = 1 + 2 f(3) = 1 + 2 + 3 ... f(n) = 1 + 2 + 3 + ... + n
17. 17. f(1) = 1 f(2) = f(1) + 2 f(3) = f(2) + 3 ... f(n) = f(n - 1) + n f(1) = 1 f(n) = f(n - 1) + n
18. 18. def f(n: Int): Int = if (n == 1) 1 else f(n - 1) + n f(1) = 1 f(n) = f(n - 1) + n
19. 19. f(0) = 1 f(1) = 1 f(2) = 2 * 1 f(3) = 3 * 2 * 1 ... f(n) = n * ... * 3 * 2 * 1
20. 20. f(0) = 1 f(1) = 1 * f(0) f(2) = 2 * f(1) f(3) = 3 * f(2) ... f(n) = n * f(n - 1)
21. 21. f(0) = 1 f(n) = n * f(n - 1) def f(n: Int): Int = if (n == 0) 1 else n * f(n - 1)
22. 22. n ? 1, 1, 2, 3, 5, 8, 13, …
23. 23. f(0) = 1 f(1) = 1 f(2) = 1 + 1 f(3) = 1 + 2 f(4) = 2 + 3 f(5) = 3 + 5 ...
24. 24. f(0) = 1 f(1) = 1 f(2) = f(0) + f(1) f(3) = f(1) + f(2) f(4) = f(2) + f(3) f(5) = f(3) + f(4) ... f(n) = f(n - 2) + f(n - 1)
25. 25. f(0) = 1 f(1) = 1 f(n) = f(n - 2) + f(n - 1) def f(n: Int): Int = if (n == 0) 1 else if (n == 1) 1 else f(n - 2) + f(n - 1)
26. 26. sum def sum(ints: List[Int]): Int
27. 27. • Nil •head tail head :: tail 3 Nil Nil:: 3 :: Nil2 :: 2 :: 3 :: Nil1 :: • •
28. 28.
29. 29. 5 :: 1 :: 2 :: 8 :: Nil Nil Nil::8 8 :: Nil::2 2 :: 8 :: Nil::1 1 :: 2 :: 8 :: Nil::5
30. 30. sum(5 :: 1 :: 2 :: 8 :: Nil) sum( ) = 0 sum( ) = 8 + 0 sum( ) = 2 + 8 + 0 sum( ) = 1 + 2 + 8 + 0 sum( ) = 5 + 1 + 2 + 8 + 0 Nil::8 Nil 8 :: Nil::2 2 :: 8 :: Nil::1 1 :: 2 :: 8 :: Nil::5
31. 31. sum( ) = 0 sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = + sum( ) sum( )  = + sum( ) Nil::8 Nil 8 :: Nil::2 2 :: 8 :: Nil::1 1 :: 2 :: 8 :: Nil::5 Nil8 8 :: Nil2 2 :: 8 :: Nil1 5 1 :: 2 :: 8 :: Nil
32. 32. sum( ) = 0 sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = + sum( ) Nil::8 Nil 8 :: Nil::2 2 :: 8 :: Nil::1 Nil8 8 :: Nil2 2 :: 8 :: Nil1 head tail head tail head tail head tail head :: tail
33. 33. sum(Nil) = 0 sum(head :: tail) = head + sum(tail) def sum(list: List[Int]): Int = if (list.isEmpty) 0 else list.head + sum(list.tail) def sum(list: List[Int]): Int = list match { case Nil => 0 case head :: tail => head + sum(tail) }
34. 34. def sum(list: List[Int]): Int = list match { case Nil => 0 case head :: tail => head + sum(tail) } def sum(list: List[Int]): Int = { def loop(acc: Int, l: List[Int]): Int = l match { case Nil => acc case head :: tail => loop(acc + head, tail) } loop(0, list) }
35. 35. product def product(ints: List[Int]): Int
36. 36. max def max(ints: List[Int]): Int
37. 37. reverse def reverse(ints: List[Int]): List[Int]
38. 38. length def length(ints: List[Int]): Int
39. 39. • Functional Programming Principles in Scala  Scala Martin Odersky   https://www.coursera.org/course/progfun •   OCaml   http://www.amazon.co.jp/dp/4781911609
40. 40. • Scala & ― Scalaz   2   http://www.amazon.co.jp/dp/4844337769
• #### umablue

Oct. 8, 2016

Scala 関西 Summit 2016 のハンズオンで使ったスライド

Total views

1,296

On Slideshare

0

From embeds

0

Number of embeds

441

2

Shares

0