Is there a flow operator to consolidate consecutiv...
# getting-started
m
Is there a flow operator to consolidate consecutive items under certain conditions? For example, suppose there is a flow of Strings and I want to concatenate consecutive single character strings like this: (‘a’, ‘bc’, ‘d’, ‘e’, ‘f’, ‘gh’) to (‘a’, ‘bc’, ‘def’, ‘gh’)
m
sorry, no. you can write one yourself using fold and rules
Copy code
val list = listOf("a", "bc", "d", "e", "f", "gh")
    
    val result = list.fold(mutableListOf<StringBuilder>() to false){ (list, buildingOnes), s ->
        list.apply {
            when{
                s.length == 1 && buildingOnes -> last().append(s)
                else -> add(StringBuilder(s))
            }
        } to (s.length == 1)
    }.first.map(StringBuilder::toString)

    result.forEach(::println)
👍 1