Nino
08/02/2023, 9:43 AMval modeTicks: List<Pair<Int, LocalTime>> = listOf(
0 to LocalTime.of(11, 0), // Start at 11:00 with mode 0
0 to LocalTime.of(11, 5), // Mode 0 again at 11:05 (same mode as previous can be reemitted because reasons)
1 to LocalTime.of(11, 7), // Switch to mode 1 at 11:07
2 to LocalTime.of(11, 8), // Switch to mode 2 at 11:08
2 to LocalTime.of(11, 11), // "End" tick finishing mode is guaranteed
)
At the end, I want a Map<Int, Duration> that have as a key 0, 1 or 2, and the values should be the Duration each mode spent on. So the result should be:
{0=7m, 1=1m, 2=3m}
My implementation (playground): https://pl.kotl.in/PiIQ--HDs
Any way to improve it ? Thanks šSam
08/02/2023, 9:48 AMval builder = SomeBuilder()
modeTicks.forEach { (mode, time) -> builder.changeMode(mode, time) }
builder.endOfInput()
val result = builder.toMap()
implementation of SomeBuilder left as an exercise for the reader šNino
08/02/2023, 9:50 AMWout Werkman
08/02/2023, 9:51 AMzipWithNext() and map
.zipWithNext { previous, current ->
previous.mode to JavaDuration.between(previous.localTime, current.localTime).toKotlinDuration()
}Klitos Kyriacou
08/02/2023, 3:29 PMval assistanceModeDurations: Map<Int, Duration> = modeTicks
.filterIndexed { i, (mode, _) ->
(i == 0 || i == modeTicks.lastIndex || mode > modeTicks[i - 1].mode)
}
.zipWithNext { previous, current ->
previous.mode to JavaDuration.between(previous.localTime, current.localTime).toKotlinDuration()
}
.toMap()Michael de Kaste
08/03/2023, 7:53 AM