Taro Nagasawa
June 05, 2015
4k

# Kotlinらしいコード #jkug

https://kotlin.doorkeeper.jp/events/23515

June 05, 2015

## Transcript

ࡀɺֈ࠲ɺͱ͕େ޷͖

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

ػೳଟͯ͘ ࢖͍͜ͳͤΜ ͱ͸ݴͬͯ΋ʜ

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)! } JGFMTF͸ࣜ
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)! } ୯Ұࣜؔ਺

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 } ؔ਺Ϧςϥϧ

+ x }

+ x } ֦ுؔ਺

25. ### ୊ࡐ1. sum ·ͱΊ w WBSݏͩ w UBJM3FDVSTJWFͰ5\$0 w ϩʔΧϧؔ਺ w

JGFMTF͸ࣜ w GVOIPHF  Ͱ୯Ұࣜؔ਺ w ֦ுؔ਺ w ඪ४"1*Λ࢖͏

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

String): Unit

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

} OVMMνΣοΫ ͜ͷதͰOBNF͸ OPU/VMMͱͯ͠ѻ͑Δ

37. ### ୊ࡐ2. nullable ·ͱΊ w OVMMνΣοΫ͍͢͝ w ͷ҆શݺͼग़͠ศར w MFUؔ਺໘ന͍ w

҉໧ͷม਺JU w ؔ਺ࢀর w ͓·͚IUUQTTQFBLFSEFDLDPNOUBSP LPUMJOOVNCFSTIJCVJHVKWN

39. ### 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)! }!
40. ### val taro = User.Builder()! .id(123)! .name(“Taro”)! ! .build()! ! val

jiro = taro.builder()! .name(“Jiro”)! .build() qVFOUJOUFSGBDFͰ JNNVUBCMFͳ΍ͭ

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

EBUBΞϊςʔγϣϯ ϓϩύςΟ

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

EBUBΞϊςʔγϣϯʹΑΔ DPQZϝιου

48. ### 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)! }
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)! } \$IBS4FRVFODFʹ IFMMPϝιουͷ௥Ճ͕͍ͨ͠
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)! } ͦͷଞͷϝιου͸ ͦͷ··ϥοϓͯ͠Δ࣮૷ʹ ҕৡ
51. ### class GreetableCharSeq! (private val cs: CharSequence): CharSequence by cs {!

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

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