首页 > 解决方案 > 谓词为真时拆分列表

问题描述

当特定谓词为真时,Kotlin 是否提供突变函数来拆分列表?

在下面的示例中,当元素是 a 时,列表应该被拆分.。结果应该是类型List<List<String>>

// input list
val list = listOf(
    "This is", "the", "first sentence", ".",
    "And", "now there is", "a second", "one", ".",
    "Nice", "."
)

// the following should be the result of the transformation
listOf(
    listOf("This is", "the", "first sentence"),
    listOf("And", "now there is", "a second", "one"),
    listOf("Nice")
)

我需要类似的东西list.splitWhen { it == "." }

标签: listkotlintransformation

解决方案


当特定谓词为真时,Kotlin 是否提供突变函数来拆分列表?

我听说过的最接近的是partition(),但我认为它不适用于您的情况。

我已经制作并简要测试了 3 个高阶扩展函数,它们给出了相同的预期输出。

解决方案 1:直截了当的方法

inline fun List<String>.splitWhen(predicate: (String)->Boolean):List<List<String>> {
    val list = mutableListOf<MutableList<String>>()
    var needNewList = false
    forEach {
            string->
        if(!predicate(string)){
            if(needNewList||list.isEmpty()){
                list.add(mutableListOf(string))
                needNewList= false
            }
            else {
                list.last().add(string)
            }
        }
        else {
            /* When a delimiter is found */
           needNewList = true
        }
    }
    return list
 }

解决方案 2:基于配对的方法

inline fun List<String>.splitWhen(predicate: (String)->Boolean):List<List<String>> {
    val list = mutableListOf<List<String>>()
    withIndex()
        .filter { indexedValue ->  predicate(indexedValue.value) || indexedValue.index==0 || indexedValue.index==size-1} // Just getting the delimiters with their index; Include 0 and last -- so to not ignore it while pairing later on
        .zipWithNext() // zip the IndexValue with the adjacent one so to later remove continuous delimiters; Example: Indices : 0,1,2,5,7 -> (0,1),(1,2),(2,5),(5,7)
        .filter { pair-> pair.first.index + 1 != pair.second.index } // Getting rid of continuous delimiters; Example: (".",".") will be removed, where "." is the delimiter
        .forEach{pair->
            val startIndex = if(predicate(pair.first.value)) pair.first.index+1 else pair.first.index // Trying to not consider delimiters
            val endIndex = if(!predicate(pair.second.value) && pair.second.index==size-1) pair.second.index+1 else pair.second.index // subList() endIndex is exclusive
            list.add(subList(startIndex,endIndex)) // Adding the relevant sub-list
        }
    return list
}

解决方案 3:如果找到分隔符,则检查下一个值

inline fun List<String>.splitWhen(predicate: (String)-> Boolean):List<List<String>> =
foldIndexed(mutableListOf<MutableList<String>>(),{index, list, string->
    when {
        predicate(string) -> if(index<size-1 && !predicate(get(index+1))) list.add(mutableListOf()) // Adds  a new List within the output List; To prevent continuous delimiters -- !predicate(get(index+1))
        list.isNotEmpty() -> list.last().add(string) // Just adding it to lastly added sub-list, as the string is not a delimiter
        else -> list.add(mutableListOf(string)) // Happens for the first String
    }
    list})

只需调用list.splitWhen{it=="delimiter"}. 解决方案 3 看起来更像是语法糖。除此之外,您可以进行一些性能测试以检查哪一个性能良好。

注意:我做了一些简短的测试,你可以通过Kotlin Playground或 Github gist看看。


推荐阅读