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

Kotlin入門

 Kotlin入門

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

magiepooh

June 14, 2017
Tweet

More Decks by magiepooh

Other Decks in Technology

Transcript

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

    2012೥ࠒ M1ϦϦʔε • 2016೥ 1.0 ϦϦʔε • Support JS(Kotlin/JS) • LLVM(Kotlin/Native)
  2. Kotlin • Lambdas -> Retrolambda • Nullable(NonNull) -> Optional •

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

    High-Order Functions(Stream) -> StremeAPI, RxJava • Extension Functions
  4. val num : Int = 1
 var num2 : Int

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

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

    => ίϯύΠϧΤϥʔ
 
 var b: String? = "abc"
 b = null // => OK
  7. 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
  8. Null҆શʢΤϧϏεԋࢉࢠʣ var b: String? = "abc"
 val l : Int

    = if(b != null) b.length else -1 val l : Int = b?.length ?: -1
  9. ߴ֊ؔ਺ // body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ
 fun <T> lock(lock: Lock, body: ()

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

    -> T): T {
 lock.lock()
 try {
 // Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ
 return body()
 } finally {
 lock.unlock()
 }
 }
  11. ϥϜμࣜ max(strings, { a, b -> a.length < b.length })


    max(strings) { a, b -> a.length < b.length }
  12. 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();
 }
  13. 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()
 }
  14. 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();
 }
  15. είʔϓؔ਺ୡ(ൈਮ) 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)
  16. είʔϓؔ਺(run) public inline fun <T, R> T.run(block: T.() -> R):

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

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

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

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

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

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

    { block(); return this } val s = "hoge".apply { toUpperCase() }
 println(s) //=> hoge toUpperCase()Λ͍ͯ͠Δ͕ɺ “hoge” ͕ฦ͞ΕΔͷͰ஫ҙ
  23. είʔϓؔ਺(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 ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ਎
  24. είʔϓؔ਺(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ͰΞΫηεՄೳ
  25. είʔϓؔ਺(let) public inline fun <T, R> T.let(block: (T) -> R):

    R = block(this) val upperCase: String? = foo?.let { it.toUpperCase() }
  26. 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()
 }
  27. fun toJSON(collection: Collection<Int>): String =
 StringBuilder().run {
 append("[")
 collection.iterator().also {


    while (it.hasNext()) {
 append(it.next())
 if (it.hasNext()) {
 append(", ")
 }
 }
 }
 append("]")
 toString()
 }
  28. 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ʢ੺΂͜ຊʣ
  29. ͜Ε͔Βͷֶश • 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ʢ੺΂͜ຊʣ