English 中文(简体)
我期待的是哪些流动经营者。
原标题:What Flow operator am I looking for

附录一 List<Person>和<编码>getStudent (ssn: String): Student

data class Person(val name: String, val ssn: String)
data class Student(val ssn: String, val grade: Char)
data class ReportCard(val name: String, val grade: Char)

我想有一个<代码>List<ReportCard>,但我不想等到成为报告卡。 例如,获得[(“Joy”、“111”)、“Tom”、“123”、“Jerry”、“567”]。 假设所挣的职等是:

Joy: C Jerry: F Tom: A

我会同一位学生一起,在一份滚动清单中编造一个报告中心,因此,我希望报告Card的田地以同步方式填满。 如果出现错误,我就忽视并恢复。

退学:

[(“Joy”),(Tom), (“Jerry”,)] <-getPeople(已完成)

[(“Joy”、“A”、“Tom”、“Jerry”)] <-getStudent(Joy)

[(“Joy”、“A”、“Tom”、“Jerry”、“F)]<-getStudent(Jerry)

[("Joy", A ), ("Tom", ), ("Jerry", F )] <--getStudent(Tom) error

另一项解决办法是在各年级完成后放弃:

[(“Joy”),(Tom), (“Jerry”,)] <-getPeople(已完成)

[(“Joy”、“A”、“Tom”、“Jerry”、“F)]<-getStudent(电话)

问题回答

我认为,只有一套简单的流动运营商可以用于这一目的,因此,我们需要用<条码>流程<>条码/代码”建立起来,以获得这一功能。

此外,没有空洞的东西。 同你一样,在你看来有解决办法,因此,我将使用无效的<编码>Char <>/code>和null来代表没有级别。

介绍这些内容是你现有的投入:

val people: Flow<List<Person>> = //...

suspend fun getGradeForSsn(ssn: String): Char? { //...

首先,对于流动中的简单化,造成这一功能的错误追索方式:

suspend fun getGradeForSsnOrNull(ssn: String): Char? =
    try { 
        getGradeForSsn(ssn) 
    } catch (e: CancellationException) {
        throw(e)
    } catch (e: Exception) {
        null
    }

我已经检验了这些解决办法,但也许这些解决办法可以成为你建立行之有效的东西的开端。

接下来,如果你只为每个投入人名单提供两份报告卡,你可以这样做:

val reportCards = people.flatMapLatest { peopleList ->
        flow {
            emit(peopleList.map { ReportCard(it.name, null) })
            val finalList = coroutineContext {
                peopleList.map { 
                    async { ReportCard(it.name, getGradeForSsnOrNull(it.ssn)) }
                }.awaitAll()
            }
            emit(finalList)
        }
    }

第一个案例更为复杂。 或许可以这样说:

val reportCards = people.flatMapLatest { peopleList ->
        flow {
            val reportCardsBySsn = peopleList.associate { 
                it.ssn to ReportCard(it.name, null)
            }
            emit(reportCardsBySsn.values.toList())
            coroutineContext {
                peopleList.forEach { 
                    val grade = getGradeForSsnOrNull(it.ssn)
                    if (grade != null) {
                        reportCardsBySsn[ssn] = reportCardsBySsn[ssn].copy(grade = grade)
                        emit(reportCardsBySsn.values.toList())
                    }
                }
            }
        }
    }




相关问题
Exposed runs query on all HikariCP open connections

I m running a Ktor server with a PostgreSQL database and just set up the JetBrains Exposed ORM framework with HikariCP for connection pooling (per the recommendation in the Ktor documentation). My ...

SQLite Kotlin issue - no database

I am trying to create boardgamegeek APIs based app in AndroidStudio, however from unknown reason I m getting my database created with no columns. Here s Logical log: (1) no such table: games in "...

Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

热门标签