Find the sum of all digits that match the following digit in a circular list
foldIndexed -> fold

The foldIndexed line can be replaced with:

.fold(Carry(-1, -1, 0)) { (first, last, total), value -&gt;


This is because the indexed part is unused

Using a data class

A data class can be used to make the values passed along the fold operation more obvious.