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.

こわくない型クラス

6,959 views

Published on

  • Hi there! I just wanted to share a list of sites that helped me a lot during my studies: .................................................................................................................................... www.EssayWrite.best - Write an essay .................................................................................................................................... www.LitReview.xyz - Summary of books .................................................................................................................................... www.Coursework.best - Online coursework .................................................................................................................................... www.Dissertations.me - proquest dissertations .................................................................................................................................... www.ReMovie.club - Movies reviews .................................................................................................................................... www.WebSlides.vip - Best powerpoint presentations .................................................................................................................................... www.WritePaper.info - Write a research paper .................................................................................................................................... www.EddyHelp.com - Homework help online .................................................................................................................................... www.MyResumeHelp.net - Professional resume writing service .................................................................................................................................. www.HelpWriting.net - Help with writing any papers ......................................................................................................................................... Save so as not to lose
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating for everyone is here: ♥♥♥ http://bit.ly/39mQKz3 ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Follow the link, new dating source: ♥♥♥ http://bit.ly/39mQKz3 ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

こわくない型クラス

  1. 1. こわくない型クラス @kmizu Scala Conference in Japan 2013 座長 Scalaエヴァンジェリスト(に戻った?)
  2. 2. こわくない話
  3. 3. 型クラスは怖くない 型クラスは友達
  4. 4. 本当に?
  5. 5. 本当です(真顔)
  6. 6. 簡単な例題
  7. 7. リストの要素を全て足し合わせた値を返 す関数
  8. 8. 素直な解
  9. 9. def sum(list: List[Int]): Int = list.foldLeft(0)(_ + _)
  10. 10. これでOK?
  11. 11. Intにしか適用できない
  12. 12. 加算できる型ならなんでもsumしたい (Int,Long,Float,Doubleとか)
  13. 13. trait Monoid[A] { def plus(x: A, y: A): A def zero: A } def sum[A](list: List[A])(m: Monoid[A]): A = { list.foldLeft(m.zero) { case (total, e) => m.plus(total, e) } }
  14. 14. ここまでが準備
  15. 15. object IntMonoid extends Monoid[Int] { def plus(x: Int, y: Int): Int = x + y def zero: Int = 0 } sum(List(1, 2, 3, 4))(IntMonoid)
  16. 16. object DoubleMonoid extends Monoid[Double] { def plus(x: Double, y: Double): Double= x + y def zero: Double = 0.0 } sum(List(1.0, 2.0, 3.0, 4.0))(DoubleMonoid)
  17. 17. やった!
  18. 18. 毎回 XXXMonoid渡すの?
  19. 19. めんどくさい
  20. 20. その気持ちをわかって欲しい
  21. 21. 気持ちを伝える方法
  22. 22. trait Monoid[A] { def plus(x: A, y: A): A def zero: A } def sum[A](list: List[A])( implicit m: Monoid[A]): A = { list.foldLeft(m.zero) { case (total, e) => m.plus(total, e) } }
  23. 23. implicit object DoubleMonoid extends Monoid[Double] { def plus(x: Double, y: Double): Double= x + y def zero: Double = 0.0 } implicit object IntMonoid extends Monoid[Int] { def plus(x: Int, y: Int): Int = x + y def zero: Double = 0
  24. 24. sum(List(1, 2, 3, 4, 5)) // ⇒ 15 sum(List(1.0, 2.0, 3.0. 4.0, 5.0)) // ⇒ 15.0
  25. 25. 他の型のListもsumしたくなったら?
  26. 26. implicit object BooleanMonoid extends Monoid[Boolean] { def plus(x: Boolean, y: Boolean): Boolean = x | y def zero: Boolean = false } List(true, false, true) // ⇒ true List(false, false, false) // ⇒ true
  27. 27. 話を戻します
  28. 28. めんどくさい def sum[A](list: List[A])( implicit m: Monoid[A]): A = { list.foldLeft(m.zero) { case (total, e) => m.plus(total, e) } }
  29. 29. def sum[A:Monoid](list: List[A]): A = { list.foldLeft(m.zero) { case (total, e) => m.plus(total, e) } }
  30. 30. 型クラスという言葉 出てきましたか?
  31. 31. Monoid[A] みたいなのをScalaで 型クラスと呼んでいるだけ 厳密には説明の順番が逆だけどそこは気にしない
  32. 32. Haskellの型クラスも 原理は同じ
  33. 33. Scalaでは 少し中身が見えている
  34. 34. むしろわかりやすい
  35. 35. 詳しく知りたい人向けの参考文献 Martin Odersky, Poor Man's Type Classes, 2006 http://lampwww.epfl.ch/~odersky/talks/wg2.8boston06.pdf

×