Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlinらしいコード #jkug

Kotlinらしいコード #jkug

第3回かわいいKotlin勉強会
https://kotlin.doorkeeper.jp/events/23515

Taro Nagasawa

June 05, 2015
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. KotlinΒ͍͠ίʔυ  ୈճ͔Θ͍͍,PUMJOษڧձ ! ೔ຊ,PUMJOϢʔβάϧʔϓ
 ௕ᖒଠ࿠!OHTX@UBSP

  2. ࣗݾ঺հ w ௕ᖒଠ࿠!OHTX@UBSP w ࢓ࣄͰ͸+BWB 4DBMB "OESPJE w ࣗশ,PUMJOΤϰΝϯδΣϦετ w

    ࡀɺֈ࠲ɺͱ͕େ޷͖
  3. Έͳ͞Μ KotlinΒ͍͠ίʔυ ॻ͍ͯ·͔͢ʁ

  4. w +BWBܦݧ͋Γˠ,PUMJO؆୯ ! w ؔ਺Ϧςϥϧɺߴ֊ؔ਺ w ֦ுؔ਺ɺ%FMFHBUFE1SPQFSUZ w /6--҆શ KotlinͬΆ͍ίʔυʁ

    ػೳଟͯ͘ ࢖͍͜ͳͤΜ ͱ͸ݴͬͯ΋ʜ
  5. ຊ೔ͷςʔϚ KotlinΒ͍͠ίʔυ KotlinͷػೳΛ׆͔ͨ͠ίʔυ

  6. ୊ࡐ1. sum

  7. fun sum(xs: List<Int>): Int {! var sum = 0! for(x

    in ints) {! sum += x! }! return sum! }
  8. fun sum(xs: List<Int>): Int {! var sum = 0! for(x

    in ints) {! sum += x! }! return sum! } WBSݏͩͳʔ
  9. fun sum(xs: List<Int>): Int {! @tailRecursive! fun go(xs: List<Int>, a:

    Int): Int {! if(xs.isEmpty())! return a! else! return go(xs.drop(1), a + xs.first())! }! ! return go(xs, 0)! }
  10. fun sum(xs: List<Int>): Int {! @tailRecursive! fun go(xs: List<Int>, a:

    Int): Int {! if(xs.isEmpty())! return a! else! return go(xs.drop(1), a + xs.first())! }! ! return go(xs, 0)! } ຤ඌݺͼग़͠࠷దԽΛ༗ޮ
  11. fun sum(xs: List<Int>): Int {! @tailRecursive! fun go(xs: List<Int>, a:

    Int): Int {! if(xs.isEmpty())! return a! else! return go(xs.drop(1), a + xs.first())! }! ! return go(xs, 0)! } ϩʔΧϧؔ਺
  12. fun sum(xs: List<Int>): Int {! @tailRecursive! fun go(xs: List<Int>, a:

    Int): Int {! if(xs.isEmpty())! return a! else! return go(xs.drop(1), a + xs.first())! }! ! return go(xs, 0)! } JGͰ΋FMTFͰ΋SFUVSO ؾ࣋ͪѱ͍ʜ JGͰ΋FMTFͰ΋SFUVSO ؾ࣋ͪѱ͍ʜ
  13. fun sum(xs: List<Int>): Int {! @tailRecursive! fun go(xs: List<Int>, a:

    Int): Int {! return if(xs.isEmpty()) a! else go(xs.drop(1), a + xs.first())! }! ! return go(xs, 0)! }
  14. fun sum(xs: List<Int>): Int {! @tailRecursive! fun go(xs: List<Int>, a:

    Int): Int {! return if(xs.isEmpty()) a! else go(xs.drop(1), a + xs.first())! }! ! return go(xs, 0)! } JGFMTF͸ࣜ
  15. fun sum(xs: List<Int>): Int {! @tailRecursive! fun go(xs: List<Int>, a:

    Int): Int {! return if(xs.isEmpty()) a! else go(xs.drop(1), a + xs.first())! }! ! return go(xs, 0)! } ΋ͬͱ ͔ͬ͜Α͘Ͱ͖Δ
  16. fun sum(xs: List<Int>): Int {! @tailRecursive! fun go(xs: List<Int>, a:

    Int): Int =! if(xs.isEmpty()) a! else go(xs.drop(1), a + xs.first())! ! return go(xs, 0)! }
  17. fun sum(xs: List<Int>): Int {! @tailRecursive! fun go(xs: List<Int>, a:

    Int): Int =! if(xs.isEmpty()) a! else go(xs.drop(1), a + xs.first())! ! return go(xs, 0)! } ୯Ұࣜؔ਺
  18. fun sum(xs: List<Int>): Int =! xs.fold(0) { a, x ->

    a + x }
  19. fun sum(xs: List<Int>): Int =! xs.fold(0) { a, x ->

    a + x } ඪ४"1* ࠨ৞ΈࠐΈ
  20. fun sum(xs: List<Int>): Int =! xs.fold(0) { a, x ->

    a + x } ؔ਺Ϧςϥϧ
  21. fun List<Int>.sum(): Int =! fold(0) { a, x -> a

    + x }
  22. fun List<Int>.sum(): Int =! fold(0) { a, x -> a

    + x } ֦ுؔ਺
  23. None
  24. (sumͳΜ͔ࣗ࡞͠ͳ͍)

  25. ! ! ඪ४API fun Iterable<Int>.sum(): Int (sumͳΜ͔ࣗ࡞͠ͳ͍)

  26. ୊ࡐ1. sum ·ͱΊ w WBSݏͩ w UBJM3FDVSTJWFͰ5$0 w ϩʔΧϧؔ਺ w

    JGFMTF͸ࣜ w GVOIPHF  Ͱ୯Ұࣜؔ਺ w ֦ுؔ਺ w ඪ४"1*Λ࢖͏
  27. ୊ࡐ2. nullable

  28. // ໊લΛऔಘͰ͖Δ! fun getName(): String?! ! // อଘͰ͖Δ! fun save(s:

    String): Unit
  29. val name: String? = getName()! if(name != null) {! save(name)!

    }
  30. val name: String? = getName()! if(name != null) {! save(name)!

    } OVMMνΣοΫ ͜ͷதͰOBNF͸ OPU/VMMͱͯ͠ѻ͑Δ
  31. name?.let {! name -> save(name)! }

  32. name?.let {! name -> save(name)! } ඪ४"1* MFUؔ਺

  33. name?.let {! name -> save(name)! } ඪ४"1* MFUؔ਺ ҆શݺͼग़͠

  34. name?.let {! save(it)! }

  35. name?.let {! save(it)! } ҉໧ͷม਺ JU

  36. name?.let(::save)

  37. name?.let(::save) ؔ਺ࢀর

  38. ୊ࡐ2. nullable ·ͱΊ w OVMMνΣοΫ͍͢͝ w ͷ҆શݺͼग़͠ศར w MFUؔ਺໘ന͍ w

    ҉໧ͷม਺JU w ؔ਺ࢀর w ͓·͚IUUQTTQFBLFSEFDLDPNOUBSP LPUMJOOVNCFSTIJCVJHVKWN
  39. ୊ࡐ3. immutable

  40. class User(id: Long, name: String) {! val id = id!

    val name = name! ! class Builder {! var id: Long? = null! var name: String? = null! ! fun id(id: Long): Builder {! this.id = id! return this! }! ! fun name(name: String): Builder {! this.name = name! return this! }! ! fun build(): User {! val id = checkNotNull(id)! val name = checkNotNull(name)! return User(id, name)! }!
  41. val taro = User.Builder()! .id(123)! .name(“Taro”)! ! .build()! ! val

    jiro = taro.builder()! .name(“Jiro”)! .build() qVFOUJOUFSGBDFͰ JNNVUBCMFͳ΍ͭ
  42. data class User(! val id: Long,! val name: String! )

  43. data class User(! val id: Long,! val name: String! )

    EBUBΞϊςʔγϣϯ ϓϩύςΟ
  44. val taro = User(id=123, name=“Taro”)! ! val jiro = taro.copy(name=“Jiro”)

  45. val taro = User(id=123, name=“Taro”)! ! val jiro = taro.copy(name=“Jiro”)

    ໊લ෇͖Ҿ਺
  46. val taro = User(id=123, name=“Taro”)! ! val jiro = taro.copy(name=“Jiro”)

    EBUBΞϊςʔγϣϯʹΑΔ DPQZϝιου
  47. ୊ࡐ3. immutable ·ͱΊ w EBUBΞϊςʔγϣϯڧ͍ w ໊લ෇͖Ҿ਺ w ˢ΋͏-PNCPLܥ#VJMEFSύλʔϯཁΒͳ͍

  48. ୊ࡐ4. decorator

  49. class GreetableCharSeq! (private val cs: CharSequence): CharSequence {! ! fun

    hello() {! println("Hello, ${cs}")! }! ! override fun length(): Int = cs.length()! override fun charAt(index: Int): Char = cs.charAt(index)! override fun subSequence(start: Int, end: Int): CharSequence =! cs.subSequence(start, end)! }
  50. class GreetableCharSeq! (private val cs: CharSequence): CharSequence {! ! fun

    hello() {! println("Hello, ${cs}")! }! ! override fun length(): Int = cs.length()! override fun charAt(index: Int): Char = cs.charAt(index)! override fun subSequence(start: Int, end: Int): CharSequence =! cs.subSequence(start, end)! } $IBS4FRVFODFʹ IFMMPϝιουͷ௥Ճ͕͍ͨ͠
  51. class GreetableCharSeq! (private val cs: CharSequence): CharSequence {! ! fun

    hello() {! println("Hello, ${cs}")! }! ! override fun length(): Int = cs.length()! override fun charAt(index: Int): Char = cs.charAt(index)! override fun subSequence(start: Int, end: Int): CharSequence =! cs.subSequence(start, end)! } ͦͷଞͷϝιου͸ ͦͷ··ϥοϓͯ͠Δ࣮૷ʹ ҕৡ
  52. class GreetableCharSeq! (private val cs: CharSequence): CharSequence by cs {!

    fun hello() {! println("Hello, ${cs}")! }! }
  53. class GreetableCharSeq! (private val cs: CharSequence): CharSequence by cs {!

    fun hello() {! println("Hello, ${cs}")! }! } CZͰҕৡ ҕৡ͢Δ͚ͩͷ ϝιου͸هड़ෆཁ
  54. ࣮ࡍ͸… fun CharSequence.hello() {! println(“Hello, $this”)! }

  55. ୊ࡐ4. decorator ·ͱΊ w EFDPSBUPSύλʔϯΈ͍ͨͳͷ͍ͨ͠ͱ͖͸ CZΩʔϫʔυʹΑΔҕৡͷ࢓૊ΈΛ࢖͏ w ࠓճͷྫͰ͸֦ுؔ਺ͰࡁΜͩ

  56. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠