=================
== The Archive ==
=================

[Kotlin Coroutines] 18장. 핫 데이터 소스와 콜드 데이터 소스

|

introduction

콜드
컬렉션 (List, Set)Sequence, Stream
ChannelFlow, RxJava 스트림

핫 vs 콜드

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fun main() {
    val l =
        buildList {
            repeat(3) {
                add("User$it")
                println("L: Added User")
            }
        }

    val l2 =
        l.map {
            println("L: Processing")
            "Processed $it"
        }

    val s =
        sequence {
            repeat(3) {
                yield("User$it")
                println("S: Added User")
            }
        }

    val s2 =
        s.map {
            println("S: Processing")
            "Processed $it"
        }
}

// L: Added User
// L: Added User
// L: Added User
// L: Processing
// L: Processing
// L: Processing
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private fun m(i: Int): Int {
    print("m$i ")
    return i * i
}

private fun f(i: Int): Boolean {
    print("f$i ")
    return i >= 10
}

fun main() {
    listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .map { m(it) }
        .find { f(it) }
        .let { print(it) }
    // m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 f1 f4 f9 f16 16

    println()

    sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .map { m(it) }
        .find { f(it) }
        .let { print(it) }
    // m1 f1 m2 f4 m3 f9 m4 f16 16
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private fun m(i: Int): Int {
    print("m$i ")
    return i * i
}

fun main() {
    val l =
        listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
            .map { m(it) } // m1 m2 m3 m4 m5 m6 m7 m8 m9 m10

    println(l) // [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    println(l.find { it > 10 }) // 16
    println(l.find { it > 10 }) // 16
    println(l.find { it > 10 }) // 16

    val s =
        sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
            .map { m(it) }

    println(s.toList())
    // [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    println(s.find { it > 10 }) // m1 m2 m3 m4 16
    println(s.find { it > 10 }) // m1 m2 m3 m4 16
    println(s.find { it > 10 }) // m1 m2 m3 m4 16
}

핫 채널, 콜드 플로우

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
val channel = produce {
    while (true) {
        val x = computeNextValue()
        send(x)
    }
}

val flow = flow {
    while (true) {
        val x = computeNextValue()
        emit(x)
    }
}

핫 채널

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@OptIn(ExperimentalCoroutinesApi::class)
private fun CoroutineScope.makeChannel() =
    produce {
        println("Channel started")
        for (i in 1..3) {
            delay(1000)
            send(i)
        }
    }

suspend fun main() =
    coroutineScope {
        val channel = makeChannel()

        delay(1000)
        println("Calling channel...")
        for (value in channel) {
            println(value)
        }
        println("Consuming again...")
        for (value in channel) {
            println(value)
        }
    }

// Channel started
// (1 sec)
// Calling channel...
// 1
// (1 sec)
// 2
// (1 sec)
// 3
// Consuming again...

콜드 플로우

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private fun makeFlow() =
    flow {
        println("Flow started")
        for (i in 1..3) {
            delay(1000)
            emit(i)
        }
    }

suspend fun main() =
    coroutineScope {
        val flow = makeFlow()

        delay(1000)
        println("Calling flow...")
        flow.collect { value -> println(value) }
        println("Consuming again...")
        flow.collect { value -> println(value) }
    }

// (1 sec)
// Calling flow...
// Flow started
// (1 sec)
// 1
// (1 sec)
// 2
// (1 sec)
// 3
// Consuming again...
// Flow started
// (1 sec)
// 1
// (1 sec)
// 2
// (1 sec)
// 3

요약