magiepooh
June 14, 2017
1.7k

# Kotlin入門

スコープ関数の理解をゴールとしたKotlinの入門資料

June 14, 2017

## Transcript

5. ### Kotlin • Jet Brains ͕࡞ͬͨJVMݴޠ • ੩తܕ෇͚ͷΦϒδΣΫτࢦ޲ݴޠ • GoogleIOͰAndroidͷ։ൃݴޠͱͯ͠ެࣜαϙʔτ͕ൃද •

2012೥ࠒ M1ϦϦʔε • 2016೥ 1.0 ϦϦʔε • Support JS(Kotlin/JS) • LLVM(Kotlin/Native)

Functions
7. ### Kotlin • Lambdas -> Retrolambda • Nullable(NonNull) -> Optional •

High-Order Functions(Stream) -> StremeAPI, RxJava • Extension Functions
8. ### Kotlin • Lambdas -> Retrolambda • Nullable(NonNull) -> Optional •

High-Order Functions(Stream) -> StremeAPI, RxJava • Extension Functions

with

11. ### val num : Int = 1  var num2 : Int

= 2    num = 3 // error  num2 = 3 ม਺ͷએݴ
12. ### val num = 1  var num2 = 2    num

= 3 // error  num2 = 3 ม਺ͷએݴ
13. ### Null҆શ var a: String = "abc"  a = null //

=> ίϯύΠϧΤϥʔ    var b: String? = "abc"  b = null // => OK
14. ### Null҆શʢݺͼग़͠ʣ var b: String? = "abc"  val l : Int

= b.length // => ίϯύΠϧΤϥʔ    var b: String? = "abc"  val l : Int = if(b != null) {  b.length  } else {  -1  }   val l : Int? = b?.length val l : Int = b?.length ?: -1
15. ### Null҆શʢΤϧϏεԋࢉࢠʣ var b: String? = "abc"  val l : Int

= if(b != null) b.length else -1 val l : Int = b?.length ?: -1

+ y  }

y

21. ### ߴ֊ؔ਺ // body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ  fun <T> lock(lock: Lock, body: ()

-> T): T {  lock.lock()  try {  // Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ  return body()  } finally {  lock.unlock()  }  }
22. ### ߴ֊ؔ਺ // body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ  fun <T> lock(lock: Lock, body: ()

-> T): T {  lock.lock()  try {  // Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ  return body()  } finally {  lock.unlock()  }  }

24. ### ϥϜμࣜ max(strings, { a, b -> a.length < b.length })

max(strings) { a, b -> a.length < b.length }

== 0

== 0 Ϩγʔό
29. ### ֦ுؔ਺ "hoge".isEmpty() @kotlin.internal.InlineOnly  public inline fun CharSequence.isEmpty(): Boolean = length

== 0 this.length: this ͸ϨγʔόΦϒδΣΫτʹରԠ

31. ### public String toJSON(Collection<Integer> collection) {  StringBuilder sb = new StringBuilder();

sb.append("[");  Iterator<Integer> iterator = collection.iterator();  while (iterator.hasNext()) {  Integer element = iterator.next();  sb.append(element);  if (iterator.hasNext()) {  sb.append(", ");  }  }  sb.append("]");  return sb.toString();  }

33. ### fun toJSON(collection: Collection<Int>): String {  val sb = StringBuilder()  sb.append("[")

val iterator = collection.iterator()  while (iterator.hasNext()) {  val element = iterator.next()  sb.append(element)  if (iterator.hasNext()) {  sb.append(", ")  }  }  sb.append("]")  return sb.toString()  }
34. ### public String toJSON(Collection<Integer> collection) {  StringBuilder sb = new StringBuilder();

sb.append("[");  Iterator<Integer> iterator = collection.iterator();  while (iterator.hasNext()) {  Integer element = iterator.next();  sb.append(element);  if (iterator.hasNext()) {  sb.append(", ");  }  }  sb.append("]");  return sb.toString();  }

36. ### είʔϓؔ਺ୡ(ൈਮ) public inline fun <T, R> T.run(block: T.() -> R):

R = block() public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block() public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this } public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this } public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
37. ### είʔϓؔ਺(run) public inline fun <T, R> T.run(block: T.() -> R):

R = block() val s = "hoge".run { toUpperCase() }  println(s) //=> HOGE ೚ҙͷܕTΛϨγʔόͱ͠ɺ ೚ҙͷܕRΛฦؔ͢਺
38. ### είʔϓؔ਺(run) public inline fun <T, R> T.run(block: T.() -> R):

R = block() val s = "hoge".run { toUpperCase() }  println(s) //=> HOGE ϨγʔόΦϒδΣΫτʹ͸thisͰΞΫηεՄೳ ʢলུ͍ͯ͠Δʣ
39. ### είʔϓؔ਺(with) public inline fun <T, R> with(receiver: T, block: T.()

-> R): R = receiver.block() val s = with("hoge") { this.toUpperCase() }  println(s) //=> HOGE
40. ### είʔϓؔ਺(with) public inline fun <T, R> with(receiver: T, block: T.()

-> R): R = receiver.block() val s = with("hoge") { toUpperCase() }  println(s) //=> HOGE ֦ுؔ਺Ͱ͸ͳ͍
41. ### είʔϓؔ਺(with) public inline fun <T, R> with(receiver: T, block: T.()

-> R): R = receiver.block() val s = with("hoge") { toUpperCase() }  println(s) //=> HOGE ϨγʔόΦϒδΣΫτʹ͸thisͰΞΫηεՄೳ ʢলུ͍ͯ͠Δʣ
42. ### είʔϓؔ਺(apply) public inline fun <T> T.apply(block: T.() -> Unit): T

{ block(); return this } val s = "hoge".apply { toUpperCase() }  println(s) //=> hoge ໭Γ஋͸this ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ਎
43. ### είʔϓؔ਺(apply) public inline fun <T> T.apply(block: T.() -> Unit): T

{ block(); return this } val s = "hoge".apply { toUpperCase() }  println(s) //=> hoge toUpperCase()Λ͍ͯ͠Δ͕ɺ “hoge” ͕ฦ͞ΕΔͷͰ஫ҙ

}
45. ### είʔϓؔ਺(also) public inline fun <T> T.also(block: (T) -> Unit): T

{ block(this); return this } val s = "hoge".also { it.toUpperCase() }  println(s) //=> hoge ໭Γ஋͸this ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ਎
46. ### είʔϓؔ਺(also) public inline fun <T> T.also(block: (T) -> Unit): T

{ block(this); return this } val s = "hoge".also { it.toUpperCase() }  println(s) //=> hoge Ҿ਺ͱͯ͠T͕౉ͬͯ͘ΔͨΊɺ itͰΞΫηεՄೳ
47. ### είʔϓؔ਺(let) public inline fun <T, R> T.let(block: (T) -> R):

R = block(this) val upperCase: String? = foo?.let { it.toUpperCase() }

49. ### fun toJSON(collection: Collection<Int>): String {  val sb = StringBuilder()  sb.append("[")

val iterator = collection.iterator()  while (iterator.hasNext()) {  val element = iterator.next()  sb.append(element)  if (iterator.hasNext()) {  sb.append(", ")  }  }  sb.append("]")  return sb.toString()  }
50. ### fun toJSON(collection: Collection<Int>): String =  StringBuilder().run {  append("[")  collection.iterator().also {

while (it.hasNext()) {  append(it.next())  if (it.hasNext()) {  append(", ")  }  }  }  append("]")  toString()  }

52. ### Next Step • Slack • kotlinlang: http://slack.kotlinlang.org/ • kotlinlang-jp: http://kotlinlang-jp.herokuapp.com/

• Try Kotlin: https://try.kotlinlang.org/ • σίϯύΠϧ • JS, Server: https://github.com/Kotlin/kotlin-fullstack-sample/ • ຊ • ॿ૸ಡຊʢແྉެ։ͷPDFʣ: https://goo.gl/5vUT7o • Kotlinʢ੺΂͜ຊʣ
53. ### ͜Ε͔Βͷֶश • Slack • kotlinlang: http://slack.kotlinlang.org/ • kotlinlang-jp: http://kotlinlang-jp.herokuapp.com/ •

Try Kotlin: https://try.kotlinlang.org/ • σίϯύΠϧ • JS, Server: https://github.com/Kotlin/kotlin-fullstack-sample/ • ຊ • ॿ૸ಡຊʢແྉެ։ͷPDFʣ: https://goo.gl/5vUT7o • Kotlinʢ੺΂͜ຊʣ