madsbf
11/05/2017, 2:38 PMval totalDistance = locations.foldIndexed(0.0) { index, distance, from ->
if (index < locations.size - 1) {
val to = locations[index + 1]
distance + from.distanceTo(to)
} else {
distance
}
}
This doesn't seem very nice to me. I have to call the list outside the scope of the block, and I have to check that I am not at the last element in the list. Do you have any ideas for a better way to do this?
Possibly, I should write my own extension method for the List, that allowed me to do something like below, but I don't know if something similar already exists?
val totalDistance = locations.sumBy { (from, to) ->
from.distanceTo(to)
}