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. fun sum(xs: List<Int>): Int {! var sum = 0! for(x

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

    in ints) {! sum += x! }! return sum! } WBSݏͩͳʔ
  3. 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)! }
  4. 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)! } ຤ඌݺͼग़͠࠷దԽΛ༗ޮ
  5. 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)! } ϩʔΧϧؔ਺
  6. 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 ؾ࣋ͪѱ͍ʜ
  7. 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)! }
  8. 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͸ࣜ
  9. 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)! } ΋ͬͱ ͔ͬ͜Α͘Ͱ͖Δ
  10. 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)! }
  11. 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)! } ୯Ұࣜؔ਺
  12. fun sum(xs: List<Int>): Int =! xs.fold(0) { a, x ->

    a + x } ඪ४"1* ࠨ৞ΈࠐΈ
  13. ୊ࡐ1. sum ·ͱΊ w WBSݏͩ w UBJM3FDVSTJWFͰ5$0 w ϩʔΧϧؔ਺ w

    JGFMTF͸ࣜ w GVOIPHF  Ͱ୯Ұࣜؔ਺ w ֦ுؔ਺ w ඪ४"1*Λ࢖͏
  14. val name: String? = getName()! if(name != null) {! save(name)!

    } OVMMνΣοΫ ͜ͷதͰOBNF͸ OPU/VMMͱͯ͠ѻ͑Δ
  15. ୊ࡐ2. nullable ·ͱΊ w OVMMνΣοΫ͍͢͝ w ͷ҆શݺͼग़͠ศར w MFUؔ਺໘ന͍ w

    ҉໧ͷม਺JU w ؔ਺ࢀর w ͓·͚IUUQTTQFBLFSEFDLDPNOUBSP LPUMJOOVNCFSTIJCVJHVKWN
  16. 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)! }!
  17. val taro = User.Builder()! .id(123)! .name(“Taro”)! ! .build()! ! val

    jiro = taro.builder()! .name(“Jiro”)! .build() qVFOUJOUFSGBDFͰ JNNVUBCMFͳ΍ͭ
  18. 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)! }
  19. 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ϝιουͷ௥Ճ͕͍ͨ͠
  20. 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)! } ͦͷଞͷϝιου͸ ͦͷ··ϥοϓͯ͠Δ࣮૷ʹ ҕৡ
  21. class GreetableCharSeq! (private val cs: CharSequence): CharSequence by cs {!

    fun hello() {! println("Hello, ${cs}")! }! } CZͰҕৡ ҕৡ͢Δ͚ͩͷ ϝιου͸هड़ෆཁ