Might there be a better way to achieve this? ``` ...
# codereview
d
Might there be a better way to achieve this?
Copy code
val elementChanges = listOf("a", "b", "b").zipWithNext { a, b ->
            if(b != a) 1 else 0
        }.sum()
I try to get the count of "changes" in a list in relation to one element to the next: Here for example I see that there is a change from "a" to "b", but after that there are no changes anymore "b" to "b"
p
Looks good to me. Other ways to write it would be:
Copy code
listOf("a", "b", "b")
    .zipWithNext { a, b -> a != b }
    .count { it }
Copy code
listOf("a", "b", "b")
    .zipWithNext()
    .count { (a, b) -> a != b }
1
d
Thank you for your suggestions! They are also good possibilities
m
I would probably do
Copy code
listOf("a", "b", "b")
    .windowed(size = 2, step = 1)
    .count { it.distinct().size == 2 }
because then the "windowSize" (2 in this case) could be extracted to parameter
d
Also a good option, thank you!