如何以更简洁的方式编写转换
How can I write the transformation in a cleaner way
我有一个获取 object(小部件)列表的方法——其中包含一些属性(header)和嵌套列表(组件)。我想将列表展平为一个列表,并使用以下代码:
@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
}
}
}
}
}
}
我打算 return 单列表:Single<MutableList<IUiData>>
从这个方法,这个目的现在可以服务,但我正在寻找更干净的方法
您以非预期的方式同时使用了 Rx 的 Observable map
和 Kotlin 的 Iterable map
。它们用于将一种类型转换为另一种类型,而不是用于迭代某些东西。
您还在最外层的 map 函数中嵌套了一个不必要的 Observable 可迭代对象。
你只需要映射Single的输出即可。在 map
函数中,您迭代(而不是 map
)原始列表以提取 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
使用的任何类型。
我有一个获取 object(小部件)列表的方法——其中包含一些属性(header)和嵌套列表(组件)。我想将列表展平为一个列表,并使用以下代码:
@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
}
}
}
}
}
}
我打算 return 单列表:Single<MutableList<IUiData>>
从这个方法,这个目的现在可以服务,但我正在寻找更干净的方法
您以非预期的方式同时使用了 Rx 的 Observable map
和 Kotlin 的 Iterable map
。它们用于将一种类型转换为另一种类型,而不是用于迭代某些东西。
您还在最外层的 map 函数中嵌套了一个不必要的 Observable 可迭代对象。
你只需要映射Single的输出即可。在 map
函数中,您迭代(而不是 map
)原始列表以提取 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
使用的任何类型。