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
What to Upload to SlideShare
Next
Download to read offline and view in fullscreen.

Share

関数プログラミングことはじめ in 福岡

Download to read offline

Scala 福岡 2019 のハンズオンで使用した資料

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

関数プログラミングことはじめ in 福岡

  1. 1. in Scala Fukuoka 2019 https://goo.gl/iKyEKs
  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. ? int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } return sum;
  12. 12. https://goo.gl/iKyEKs
  13. 13. • 1 n • int f(int n) { int total = 0; for (int i = 1; i <= n; i++) { total += i; } return total; }
  14. 14. f(1) = 1 f(2) = 1 + 2 f(3) = 1 + 2 + 3 ... f(n) = 1 + 2 + 3 + ... + n
  15. 15. 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
  16. 16. def f(n: Int): Int = if (n == 1) 1 else f(n - 1) + n f(1) = 1 f(n) = f(n - 1) + n
  17. 17. f(0) = 1 f(1) = 1 f(2) = 2 * 1 f(3) = 3 * 2 * 1 ... f(n) = n * ... * 3 * 2 * 1
  18. 18. f(0) = 1 f(1) = 1 * f(0) f(2) = 2 * f(1) f(3) = 3 * f(2) ... f(n) = n * f(n - 1)
  19. 19. f(0) = 1 f(n) = n * f(n - 1) def f(n: Int): Int = if (n == 0) 1 else n * f(n - 1)
  20. 20. n ? 1, 1, 2, 3, 5, 8, 13, …
  21. 21. f(0) = 0 f(1) = 1 f(2) = 0 + 1 f(3) = 1 + 1 f(4) = 1 + 2 f(5) = 2 + 3 ...
  22. 22. f(0) = 0 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)
  23. 23. f(0) = 0 f(1) = 1 f(n) = f(n - 2) + f(n - 1) def f(n: Int): Int = if (n == 0) 0 else if (n == 1) 1 else f(n - 2) + f(n - 1)
  24. 24. sum def sum(ints: List[Int]): Int
  25. 25. • Nil •head tail head :: tail 3 Nil Nil:: 3 :: Nil2 :: 2 :: 3 :: Nil1 :: • •
  26. 26.
  27. 27. Nil Nil::8 8 :: Nil::2 2 :: 8 :: Nil::1 1 :: 2 :: 8 :: Nil::5 5 :: 1 :: 2 :: 8 :: Nil
  28. 28. sum(5 :: 1 :: 2 :: 8 :: Nil) sum( )
 = + sum( ) sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = 0Nil 8 :: Nil 2 :: 8 :: Nil 5 :: 1 :: 2 :: 8 :: Nil Nil8 8 :: Nil2 2 :: 8 :: Nil1 5 1 :: 2 :: 8 :: Nil 1 :: 2 :: 8 :: Nil
  29. 29. sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = 0Nil 8 :: Nil 2 :: 8 :: Nil Nil8 8 :: Nil22 :: 8 :: Nil 1 head head tail head tail head tail head :: tail 1 :: 2 :: 8 :: Nil tail
  30. 30. 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) }
  31. 31. product def product(ints: List[Int]): Int
  32. 32. max def max(ints: List[Int]): Int
  33. 33. reverse def reverse(ints: List[Int]): List[Int]
  34. 34. length def length(ints: List[Int]): Int
  35. 35. sum def sum(tree: Tree): Int
  36. 36. •Node Empty Tree •Node Tree •Empty Tree 8 6 10 4 7 E 12 E E E E E E
  37. 37. Tree Node Tree Tree 8 6 10 4 7 E 12 E E E E E E
  38. 38. 8 6 10 4 7 E 12 E E E E E E sum
  39. 39. 8 6 10 4 7 E 12 E E E E E E sum sum +
  40. 40. sum(tree) tree 8 6 10 4 7 E 12 E E E E E E
  41. 41. sum(tree) = node.value
 + sum(leftTree) + sum(rightTree) leftTree rightTree + node.value 8 6 10 4 7 E 12 E E E E E E
  42. 42. sum(empty) = 0 sum(tree) = node.value + sum(left) + sum(right) trait Tree case class Node(value: Int, left: Tree, right: Tree) extends Tree case object Empty extends Tree def sum(tree: Tree): Int = tree match { case Empty => 0 case n: Node => n.value + sum(n.left) + sum(n.right) }
  43. 43. max def max(tree: Tree): Int
  44. 44. Node size def size(tree: Tree): Int
  45. 45. Node depth def depth(tree: Tree): Int
  46. 46. 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) }
  47. 47. • Functional Programming Principles in Scala
 Scala Martin Odersky 
 https://www.coursera.org/course/progfun • 
 OCaml 
 http://www.amazon.co.jp/dp/4781911609
  48. 48. • Scala & ― Scalaz 
 2 
 http://www.amazon.co.jp/dp/4844337769

Scala 福岡 2019 のハンズオンで使用した資料

Views

Total views

1,291

On Slideshare

0

From embeds

0

Number of embeds

139

Actions

Downloads

2

Shares

0

Comments

0

Likes

0

×