Use zipWithNext
.
You pass in a lambda taking 2 parameters. zipWithNext
will go through the list and pass each pair of elements to your lambda. To compute the difference, simply subtract:
val list = listOf(10, 50, 30)
val result = list.zipWithNext { a, b -> b - a }
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…