Slide 1

Slide 1 text

Kotlin'1.1'Standard'Libraryͷվળ @RyotaMurohoshi 2017/06/29(ୈ6ճ(Kotlinษڧձ(@SanSan

Slide 2

Slide 2 text

ॕ!Early!access!program!for!Kotlin!1.2!։࢝ʂ h"ps:/ /blog.jetbrains.com/kotlin/2017/06/early

Slide 3

Slide 3 text

͚ΕͲࠓ೔͸ɺKotlin'1.1ͷ࿩Λ͠·͢

Slide 4

Slide 4 text

Kotlin'1.1ͱݴ͑͹ɽɽɽ

Slide 5

Slide 5 text

Kotlin'1.1ͱݴ͑͹ • Corou&nes(experimental) • Type5aliases • Bound5callable5references • Sealed5and5data5classes ͳͲͳͲɽɽɽ

Slide 6

Slide 6 text

ͦ͏͍͏ݴޠػೳ͚ͩͰͳ͘ɺ Standard'Libraryʹ΋͍Ζ͍Ζ௥Ճ͞Ε͍ͯ·͢

Slide 7

Slide 7 text

What's'New'in'Kotlin'1.1 Standard'library'ΑΓ h"ps:/ /kotlinlang.org/docs/reference/whatsnew11.html#standard9library

Slide 8

Slide 8 text

String'to'number'conversions

Slide 9

Slide 9 text

val numString : String = loadString() // ΋͔ͨ͠͠ΒParseࣦഊͯ͠ // NumberFormatException͕౤͛ΒΕΔ͔΋ val num: Int = numString.toInt()

Slide 10

Slide 10 text

ͦ͜ͰɺtoIntOrNull

Slide 11

Slide 11 text

val numString : String = loadString() // Parseࣦഊͨ͠Βnull val num:Int? = numString.toIntOrNull()

Slide 12

Slide 12 text

// ؀ڥม਺Λύʔε͢Δ͚Ͳɺ // ࣦഊͨ͠Β80൪ʹ val port = System.getenv("PORT")?.toIntOrNull() ?: 80

Slide 13

Slide 13 text

• toDoubleOrNull • toFloatOrNull • toShortOrNull • toLongOrNull • toByteOrNull ੔਺͸ج਺Λࢦఆ͢ΔΦʔόʔϩʔυ΋͋ΔΑ

Slide 14

Slide 14 text

onEach()

Slide 15

Slide 15 text

onEach() ֤छίϨΫγϣϯͷ֦ுؔ਺ forEach͡Όͳͯ͘ɺonEachͰ͢ʂ

Slide 16

Slide 16 text

forEach val unit: Unit = listOf(1, 2, 3) .map { it * it } .forEach { println(it) } ࣮ߦ݁Ռ͸ 1 4 9

Slide 17

Slide 17 text

onEach val list: List = listOf(1, 2, 3) .map { it * it } .onEach { println(it) } ࣮ߦ݁Ռ͸ 1 4 9

Slide 18

Slide 18 text

ࣅ͍ͯ·͢Ͷ

Slide 19

Slide 19 text

val unit: Unit = listOf(1, 2, 3) .map { it * it } .forEach { println(it) } val list: List = listOf(1, 2, 3) .map { it * it } .onEach { println(it) }

Slide 20

Slide 20 text

͓Θ͔Γ͍͚ͨͩ·͔ͨ͠ʁ

Slide 21

Slide 21 text

forEach͸Unit onEach͸List

Slide 22

Slide 22 text

ίϨΫγϣϯͷϝιουνΣʔϯͷ్தͰɺ֤ཁૉʹॲཧΛߦ͏ϝιουɻ ฦΓ஋͸ݩͷίϨΫγϣϯɻ ίϨΫγϣϯࣗମͷ஋͸มΘΒͳ͍ɻ ෭࡞༻͕͋Δϝιουͱ͔΋ɻRxͷdoΈ͍ͨ

Slide 23

Slide 23 text

Sequenceͷ৔߹ɺ ཁૉ΁ͷద༻ɾධՁ͸ଈ࣌Ͱ͸ͳ͍Ͱ͢ʂ

Slide 24

Slide 24 text

val sequence: Sequence =sequenceOf(1, 2, 3) .onEach { println(it) } .map { it * it } .onEach { println(it) } println("============") for (e in sequence){ println("$e in for") }

Slide 25

Slide 25 text

============ 1 1 1 in for 2 4 4 in for 3 9 9 in for

Slide 26

Slide 26 text

ͿͬͪΌ͚ίʔυݟͨํ͕ૣ͍Ͱ͢Ͷ @SinceKotlin("1.1") public inline fun > C.onEach(action: (T) -> Unit): C { return apply { for (element in this) action(element) } }

Slide 27

Slide 27 text

also/takeIf/takeUnless

Slide 28

Slide 28 text

ίʔυݟͨํ͕Θ͔Γ΍͍͢ʂ takeIfͱtakeUnless /** * Returns `this` value if it satisfies the given [predicate] or `null`, if it doesn't. */ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null /** * Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does. */ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null

Slide 29

Slide 29 text

val evenOrNull: Int? = 2.takeIf { it % 2 == 0 } Assert.assertEquals(2, evenOrNull) val oddOrNull: Int? = 2.takeUnless { it % 2 == 0 } Assert.assertEquals(null, oddOrNull)

Slide 30

Slide 30 text

also͸applyʹࣅ͍ͯΔ ͚ΕͲ ϥϜμࣜͷதͷthisͷҙຯ͕ҧ͏

Slide 31

Slide 31 text

val bundleB = Bundle().apply { // ͜͜Ͱthis͸Bundle putString("KEY1", "VALUE1") this.putString("KEY2", "VALUE2") // ίϯύΠϧΤϥʔ //it.putString("KEY3", "VALUE3") }

Slide 32

Slide 32 text

val bundleA = Bundle().also { // ͜͜Ͱthis͸Bundleͷ֎ଆ it.putString("KEY1", "VALUE1") // ίϯύΠϧΤϥʔ //putString("KEY2", "VALUE2") // ίϯύΠϧΤϥʔ //this.putString("KEY3", "VALUE3") }

Slide 33

Slide 33 text

ίʔυݟͨํ͕Θ͔Γ΍͍͢ʂ applyͱalso /** * Calls the specified function [block] with `this` value as its receiver and returns `this` value. */ @kotlin.internal.InlineOnly public inline fun T.apply(block: T.() -> Unit): T { block(); return this } /** * Calls the specified function [block] with `this` value as its argument and returns `this` value. */ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.also(block: (T) -> Unit): T { block(this); return this }

Slide 34

Slide 34 text

groupingBy()

Slide 35

Slide 35 text

ฦΓ஋ܕ͸৽ΠϯλʔϑΣʔε!Grouping interface Grouping

Slide 36

Slide 36 text

Grouping͸άϧʔϓԽͯ͠ ͝ʹΐ͝ʹΐॲཧΛ͢ΔͨΊͷ தؒͷσʔλߏ଄Λ࢘ΔΠϯλʔϑΣʔε

Slide 37

Slide 37 text

• aggregate • fold • reduce • eachCount ͳͲͳͲ

Slide 38

Slide 38 text

groupBy͍ͬͯ͏ϝιου͋Γ·ͨ͠ΑͶʁ data class ScoreRecord( val stageId: Int, val score: Int ) val scoreRecordList = listOf( ScoreRecord(stageId = 0, score = 1), ScoreRecord(stageId = 0, score = 3), ScoreRecord(stageId = 1, score = 2), ScoreRecord(stageId = 0, score = 1), ScoreRecord(stageId = 2, score = 4) ) val map: Map> = scoreRecordList.groupBy { it.stageId }

Slide 39

Slide 39 text

groupingByͱfoldͳͲͰɺ ΋ͬͱॊೈʹ

Slide 40

Slide 40 text

val sumScores: Map = scoreRecordList .groupingBy { it.stageId } .fold(0, { acc, elm -> acc + elm.score }) val maxScores: Map = scoreRecordList .groupingBy { it.stageId } .fold(0, { acc, elm -> maxOf(acc, elm.score) })

Slide 41

Slide 41 text

toMap()(ͱ(toMutableMap()

Slide 42

Slide 42 text

toMap()(ͱ(toMutableMap() MapͷίϐʔΠϯελϯεੜ੒ϝιου val original = mutableMapOf(1 to "A", 2 to "B") val copied = original.toMap() original[3] = "C" Assert.assertEquals(3, original.size) Assert.assertEquals(2, copied.size)

Slide 43

Slide 43 text

Map.minus(key)

Slide 44

Slide 44 text

Kotlin'1.1ͷલʹ΋ɺ+ΦϖϨʔλ͸͋ͬͨ val map = mapOf(1 to "A", 2 to "B") // Kotlin 1.0Ͱ΋ // +ΦϖϨʔλʔͰཁૉΛ௥Ճͨ͠৽ͨͳMapΛੜ੒Ͱ͖ͨ val added = map + (3 to "C")

Slide 45

Slide 45 text

Kotlin'1.1͔Βɺ*ΦϖϨʔλ͕ՃΘͬͨ val map = mapOf(1 to "A", 2 to "B") // Kotlin 1.1͔Β // -ΦϖϨʔλʔͰࢦఆͨ͠ΩʔͷཁૉΛ࡟আͨ͠ // ৽ͨͳMapΛੜ੒Ͱ͖ΔΑ͏ʹ val added = map - 1

Slide 46

Slide 46 text

minOf()(ͱ(maxOf()

Slide 47

Slide 47 text

໊લͷ௨Γʂ minOf&:&Ҿ਺ʹ౉ͨ͠΋ͷ͔Β࠷খ஋Λฦ͢ maxOf&:&Ҿ਺ʹ౉ͨ͠΋ͷ͔Β࠷େ஋Λฦ͢ 2Ҿ਺όʔδϣϯͱ3Ҿ਺όʔδϣϯ͕͋Δ

Slide 48

Slide 48 text

val min = minOf(1, 2) val max = maxOf(2.3, 5.1, 4.7)

Slide 49

Slide 49 text

ComparableΛ࣮૷ͨ͠΋ͷ΋Ͱ͖Δ val min = minOf( LocalDate.parse("2017-07-21"), LocalDate.parse("2017-07-20") )

Slide 50

Slide 50 text

ൺֱ৚݅ΛҾ਺ͱͯ͠ࢦఆ΋Ͱ͖Δ val alphabetical = minOf( "Java", "Kotlin", "Android" ) val length = minOf( "Java", "Kotlin", "Android", Comparator { a, b -> a.length - b.length } )

Slide 51

Slide 51 text

Array%like*List*instan/a/on* func/ons

Slide 52

Slide 52 text

Kotlin'1.0͔Β ࣮͸഑ྻͷॳظԽɺ͜Μͳײ͡Ͱ͔͚ͨ val zeroFilledZize10 = Array(size=10) { 0 } val indexedFilled = Array(size=3) {index -> index }

Slide 53

Slide 53 text

͜ΕʹࣅͨListͱMutableListΛੜ੒͢Δ ʮؔ਺ʯ͕Kotlin'1.1͔ΒՃΘͬͨ val zeroFilledSize10 = MutableList(size=10) { 0 } val squaredIndexList = List(size = 4) { it * it }

Slide 54

Slide 54 text

େจࣈͰ࢝·Δ͚Ͳ Ϋϥε໊͡Όͳͯ͘ɺؔ਺ͷ໊લʂ

Slide 55

Slide 55 text

Map.getValue()

Slide 56

Slide 56 text

val map: Map = mapOf(1 to "A", 2 to "B") // 0ͱ͍͏Ωʔ͸ͳ͍͔ΒɺnullΛฦ͢ val valueOrNull : String? = map[0] // 0ͱ͍͏Ωʔ͸ͳ͍͔ΒɺNoSuchElementExceptionΛ౤͛Δ val value: String = map.getValue(0)

Slide 57

Slide 57 text

Abstract(collec,ons

Slide 58

Slide 58 text

Abstract(collec,ons ίϨΫγϣϯͷந৅Ϋϥε • AbstractCollec-on/ • AbstractList • AbstractSet • AbstractMap

Slide 59

Slide 59 text

Abstract(collec,ons ίϨΫγϣϯͷந৅Ϋϥε • AbstractMutableCollec/on • AbstractMutableList • AbstractMutableSet • AbstractMutableMap

Slide 60

Slide 60 text

java.u&l.AbstractCollec&onͬͯ͋Γ·ͨ͠ΑͶʁ

Slide 61

Slide 61 text

JVMͰ͸ɺ kotlin.collec*ons.MutableܥͷίϨΫγϣϯ͸ɺ java.utl.AbstractܥͷίϨΫγϣϯΛܧঝ࣮ͯ͠૷͞Ε͍ͯΔ kotlin.collec*ons.AbstractMutableCollec*on͸ɺjava.u*l.AbstractCollec*onΛܧঝ

Slide 62

Slide 62 text

JavaScriptͰ͸ɺ ͜ΕΒͷίϨΫγϣϯΛܧঝ֤ͯ͠छίϨΫγϣϯΛ࡞͍ͬͯΔ kotlin.collec*ons.ArrayList͸ɺkotlin.collec*ons.AbstractMutableListΛܧঝ͍ͯ͠Δ

Slide 63

Slide 63 text

Kotlin'1.1͔Βొ৔ kotlin.collec*ons.ArrayList JVMͰ͸ɺjava.u)l.ArrayListͷtypealias JavaScriptͰ͸ɺkotlin.collec0ons.AbstractMutableListΛܧঝͨ͠Ϋϥε

Slide 64

Slide 64 text

ίϨΫγϣϯ͚ͩ͡Όͳͯ͘ɺExcep&onܥ΋ JVMͰ͸typealiasɺJavaScriptͰ͸ಠࣗఆٛ ClassCastExcep+on,IllegalStateExcep+on

Slide 65

Slide 65 text

Array%manipula,on%func,ons

Slide 66

Slide 66 text

഑ྻͷཁૉ͝ͱ͝ʹΐ͝ʹΐ͢Δϝιου͕ Standard'Libraryʹ

Slide 67

Slide 67 text

഑ྻͷtoString() println(arrayOf(1, 2, 3).toString()) ࣮ߦ݁Ռ Ljava.lang.Integer;@46ee7fe8

Slide 68

Slide 68 text

͍΍ɺ͜Ε͕ݟ͍ͨΜ͡Όͳͯ͘orz

Slide 69

Slide 69 text

ͦ͜ͰcontentToStringͰ͢Α

Slide 70

Slide 70 text

഑ྻͷcontentToString() println(arrayOf(1, 2, 3).contentToString()) ࣮ߦ݁Ռ [1, 2, 3]

Slide 71

Slide 71 text

equalsͱcontentEquals΋ // false println(arrayOf(1, 2, 3).equals(arrayOf(1, 2, 3))) // true println(arrayOf(1, 2, 3).contentEquals(arrayOf(1, 2, 3)))

Slide 72

Slide 72 text

͍͍Ͷʂ

Slide 73

Slide 73 text

·ͱΊ

Slide 74

Slide 74 text

Standard'Library ஍ຯʹ͔ͬ͠ΓਐԽ͍ͯͨͥ͠ʂ

Slide 75

Slide 75 text

͓·͚ kotlin'stdlib'jre8ͰStream2APIͱ͔΋