https://kotlinlang.org logo
#announcements
Title
# announcements
n

nickk

08/18/2017, 1:51 PM
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

kingsley

08/18/2017, 1:55 PM
Not at the moment. However,
zipWithNext
that does exactly that was just introduced in
1.2M2
👍 1
h

hho

08/18/2017, 2:08 PM
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

arekolek

08/18/2017, 3:12 PM
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
2 Views