๊นจ์•Œ ๊ฐœ๋… ๐Ÿ“‘/Kotlin

[Kotlin] Coroutines Flow - Intermediate operator

interfacer_han 2024. 8. 16. 03:03

#1 ๊ฐœ์š”

#1-1 ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž (Intermediate operator)

 

Asynchronous Flow | Kotlin

 

kotlinlang.org

์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค.
 

#1-2 ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž์˜ ๊ตฌ์กฐ

์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž๋Š” ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ด๋ฆ„์— ์žˆ๋Š” '์ค‘๊ฐ„'์€, emit๊ณผ collect ์‚ฌ์ด '์ค‘๊ฐ„'์„ ์˜๋ฏธํ•œ๋‹ค. ์‹œ์ž‘์ ์ด emit์ด๊ณ  ๋„์ฐฉ์ ์ด collect์ธ ๋ฐฐ์ˆ˜๊ด€ ์† ๋ฌผ์˜ ํ๋ฆ„์„ intermediate operator๊ฐ€ ํ•˜์ด์žฌํ‚นํ•˜๋Š” ๋А๋‚Œ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€, ์•ž์œผ๋กœ(=๋ฏธ๋ž˜์—) emit๋  ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ˜•ํ•˜๋Š” ์—ฐ์‚ฐ์ž๋Š” ๊ฒฐ์ฝ” ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฏธ(=๊ณผ๊ฑฐ์—) emit๋œ ๋ฐ์ดํ„ฐ์— ํŠน์ • ์—ฐ์‚ฐ์„ ๊ฐ€ํ•˜์—ฌ, collect์— ์ „๋‹ฌํ•˜๋Š” ์—ฐ์‚ฐ์ž๋‹ค. ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž์ด๋‹ˆ ๋ง์ด๋‹ค.
 

#2 Flow์— ๊ฐ€์šฉํ•œ ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž ๋ชฉ๋ก

#2-1 ๋ฒ ์ด์Šค ์ฝ”๋“œ

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking

fun main() {
    // 1, 2, 3, ...์„ ๋ฐฉ์ถœํ•˜๋Š” Flow
    val numberFlow: Flow<Int> = flow {
        for (i in 1..10) {
            delay(100)
            println("emitted: $i")
            emit(i)
        }
    }

    // Flow๋ฅผ collect
    runBlocking {
        numberFlow.collect { value ->
            println("collected: $value")
        }
    }
}

/* ↑ ↑ ↑ ์ถœ๋ ฅ ๊ฒฐ๊ณผ
emitted: 1
collected: 1
emitted: 2
collected: 2
emitted: 3
collected: 3
emitted: 4
collected: 4
emitted: 5
collected: 5
emitted: 6
collected: 6
emitted: 7
collected: 7
emitted: 8
collected: 8
emitted: 9
collected: 9
emitted: 10
collected: 10
*/

๋จผ์ €, ์•„๋ฌด๋Ÿฐ ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๋ฒ ์ด์Šค ์ฝ”๋“œ๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜๋Š” ์ฝ”๋“œ๋กœ ์ˆ˜์ •ํ•ด๋ณด๊ฒ ๋‹ค.
 

#2-2 map()

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking

fun main() {
    // 1, 2, 3, ...์„ ๋ฐฉ์ถœํ•˜๋Š” Flow
    val numberFlow: Flow<Int> = flow {
        for (i in 1..10) {
            delay(100)
            println("emitted: $i")
            emit(i)
        }
    }

    // Flow๋ฅผ collect
    runBlocking {
        numberFlow
            .map { value -> // ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜
                "Hello ${value}!"
            }
            .collect { value ->
                println("collected: $value")
            }
    }
}

/* ↑ ↑ ↑ ์ถœ๋ ฅ ๊ฒฐ๊ณผ
emitted: 1
collected: Hello 1!
emitted: 2
collected: Hello 2!
emitted: 3
collected: Hello 3!
emitted: 4
collected: Hello 4!
emitted: 5
collected: Hello 5!
emitted: 6
collected: Hello 6!
emitted: 7
collected: Hello 7!
emitted: 8
collected: Hello 8!
emitted: 9
collected: Hello 9!
emitted: 10
collected: Hello 10!
*/

map()์€ emit๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ˜•ํ•˜์—ฌ collect()์— ์ „๋‹ฌํ•œ๋‹ค.
 

#2-3 filter()

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking

fun main() {
    // 1, 2, 3, ...์„ ๋ฐฉ์ถœํ•˜๋Š” Flow
    val numberFlow: Flow<Int> = flow {
        for (i in 1..10) {
            delay(100)
            println("emitted: $i")
            emit(i)
        }
    }

    // Flow๋ฅผ collect
    runBlocking {
        numberFlow
            .filter { value -> // ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทœ์น™์— ๋งž์ถฐ ๊ฑธ๋Ÿฌ๋ƒ„
                value % 3 == 0
            }
            .collect { value ->
                println("collected: $value")
            }
    }
}

/* ↑ ↑ ↑ ์ถœ๋ ฅ ๊ฒฐ๊ณผ
emitted: 1
emitted: 2
emitted: 3
collected: 3
emitted: 4
emitted: 5
emitted: 6
collected: 6
emitted: 7
emitted: 8
emitted: 9
collected: 9
emitted: 10
*/

filter()๋Š” boolean ์—ฐ์‚ฐ์„ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค. ๊ทธ ์—ฐ์‚ฐ์ด true๋ฉด collect์— ์ „๋‹ฌํ•˜๊ณ , ์•„๋‹ˆ๋ผ๋ฉด ์ „๋‹ฌํ•˜์ง€ ์•Š๋Š”๋‹ค.
 

#2-4 ๊ธฐํƒ€

 

Asynchronous Flow | Kotlin

 

kotlinlang.org

take, toList, toSet, first, single, reduce, fold ๋“ฑ ๋‹ค๋ฅธ ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž๋“ค์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ์œ„ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
 

#3 ์š”์•ฝ

์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž๋Š” ํ•˜์ด์žฌํ‚น์ด๋‹ค.