kotlin - How can I write the transformation in a cleaner way
问题描述
I have a method which takes a list of object (Widget) -- which contains some properties (header) and nested list(component). I want to flatten the list into a single list and have the below code for same:
@SuppressLint("CheckResult")
fun flatten(fatList: Single<List<Widget>>) {
val flatList: MutableList<IUiData> = mutableListOf()
fatList.map {
Observable.fromIterable(it).map { widget ->
if (widget.header.isNotEmpty()) {
flatList.add(ProductHeaderUi(widget.header))
}
widget.componentList.map { component ->
when (component.type) {
TILE_TEXT -> {
flatList.add(HeaderUi(component))
}
TILE_IMAGE -> {
flatList.add(ImageTileUi(component))
}
TILE_FOOTER -> {
flatList.add(FooterUi(component))
}
UNKNOWN -> {
//Do Nothing
}
}
}
}
}
}
I intend to return a Single of List: Single<MutableList<IUiData>>
from this method, this purpose can be served right now, but I am looking for a cleaner way
解决方案
您以一种意想不到的方式同时使用了 Rx 的 Observablemap
和 Kotlin 的 Iterable map
。它们用于将一种类型转换为另一种类型,而不是用于迭代某些东西。
您还在最外层的 map 函数中嵌套了一个不必要的 Observable 可迭代对象。
您只需要映射 Single 的输出。在map
函数内部,您迭代(而不是map
)原始 List 以提取 MutableList 所需的数据。
我是 Rx 新手,没有检查这个,对于任何语法错误,我深表歉意。
fun flatten(fatList: Single<List<Widget>>): Single<MutableList<IUData>> = fatList.map { widgetList ->
val flatList: MutableList<IUiData> = mutableListOf()
for (widget in widgetList) {
if (widget.header.isNotEmpty()) {
flatList.add(ProductHeaderUi(widget.header))
}
for (component in widget.componentList) {
when (component.type) {
TILE_TEXT -> flatList.add(HeaderUi(component))
TILE_IMAGE -> flatList.add(ImageTileUi(component))
TILE_FOOTER -> flatList.add(FooterUi(component))
// Else do nothing
}
}
}
flatList
}
但是为了与典型的 Rx 链接语法保持一致,我会将其设为扩展函数,所以我必须像这样的第一行。然后你可以把它放在 Rx 调用链的中间:
fun Single<List<Widget>>.flatten(): Single<MutableList<IUData>> = map { widgetList ->
您还可以使用 Kotlin 以更简洁、更实用但效率更低的方式执行此操作flatMap
:
fun Single<List<Widget>>.flatten(): Single<MutableList<IUData>> = map {
it.flatMap { widget ->
listOfNotNull(widget.header.takeIf(Header::isNotEmpty)?.let(::ProductHeaderUi))
+
widget.componentList.mapNotNull { component ->
when (component.type) {
TILE_TEXT -> HeaderUi(component)
TILE_IMAGE -> ImageTileUi(component)
TILE_FOOTER -> FooterUi(component)
else -> null
}
}.toMutableList()
}
...在哪里使用Header
任何类型widget.header
。
推荐阅读
- python - 如何在 HTML 中获取内容
- 从 URL 和 python
- android - 离子5记录设备的屏幕?
- powershell - Powershell 获取进程脚本无法正常工作
- c++ - 复制构造函数和带有 std::any 的构造函数之间的冲突
- vba - SaveAs 期间删除 FileName 中的字符
- android - Sqlite Table 不在小米 Note 7s 上创建,而是在 Android Studio 中的其他手机和虚拟设备上创建
- postgresql - 如何在 Azure PostgreSQL 服务器上还原 .dump 或 .sql
- r - plot.window(...) 中的错误:使用 descdist 函数时需要有限的“xlim”值
- pagination - 如何通过一些偏移量从 indexedDB 中获取条目?
- java - Java 过滤器不能返回布尔值