Is there a function in the standard lib that could...
# announcements
n
Is there a function in the standard lib that could help in taking a list of doubles and consume 2 elements each time? I want to convert a flat list of lat-lon doubles to LatLng instances.
k
Not at the moment. However,
zipWithNext
that does exactly that was just introduced in
1.2M2
👍 1
h
Workaround until then:
Copy code
val lats = latLongs.filterIndexed { index, _ -> index % 2 == 0 }
val longs = latLongs.filterIndexed { index, _ -> index % 2 != 0 }
val coords = lats.zip(longs)
👍 1
a
Or you could do:
(x.indices step 2).map { LatLng(x[it], x[it + 1]) }
BTW,
zipWithNext
returns a list of all adjacent pairs
(1..5).zipWithNext { a, b -> println("$a, $b") }
prints:
Copy code
1, 2
2, 3
3, 4
4, 5
x.chunked(2) { LatLng(it[0], it[1]) }
would do it though