voddan
06/21/2016, 2:25 PMcedric
06/21/2016, 2:26 PMvoddan
06/21/2016, 2:27 PMvalue
, which I needvoddan
06/21/2016, 2:27 PMilya.gorbunov
06/21/2016, 2:39 PMvoddan
06/21/2016, 2:43 PMdimsuz
06/22/2016, 9:00 AMArray
constructor with init
so I can do Array(5, { i -> createItem(i) })
. Is there something similar for initializing lists in stdlib
? none of listOf
overloads has any additional parameters...dimsuz
06/22/2016, 9:03 AM(0..size-1).map { i -> createItem(i) }
kirillrakhman
06/22/2016, 9:47 AMIntRange
kirillrakhman
06/22/2016, 9:48 AMList
, you can also do indices.map { createItem(it) }
kirillrakhman
06/22/2016, 9:48 AM(0..lastIndex).map { ...}
kirillrakhman
06/22/2016, 9:49 AMsize - 1
feels un-Kotlin-y and I try to avoid it when I candimsuz
06/22/2016, 10:00 AMcreateItem()
was using another list under the hood, so I just went with anotherList.map { ... }
rocketraman
06/22/2016, 2:49 PMfilterIsInstance
with withIndex
somehow? I want to filter my list using filterIsInstance
but maintain the original index in the resulting `IndexedValue`svoddan
06/22/2016, 2:50 PMlist.mapIndexed {i, v -> Pair(i, v)}.filter {it.second is A} as List<Int, A>
rocketraman
06/22/2016, 2:54 PMmap
at the end there to convert the List<Int, A>
into a List<IndexedValue<A>>
as well. Should work but not so elegant.voddan
06/22/2016, 2:56 PMList<Pair<Int, A>>
voddan
06/22/2016, 2:57 PMrocketraman
06/22/2016, 2:58 PMfun <T> List<T>.filterIsInstanceIndexed(): List<IndexedValue<T>> =
mapIndexed {i, v -> Pair(i, v)}.filter { it.second is T }.map { IndexedValue(it.first, it.second) }
rocketraman
06/22/2016, 2:58 PMvoddan
06/22/2016, 2:58 PMPair
if you need IndexedValue
?rocketraman
06/22/2016, 2:59 PMvoddan
06/22/2016, 2:59 PMrocketraman
06/22/2016, 2:59 PMfun <T> List<T>.filterIsInstanceIndexed(): List<IndexedValue<T>> =
mapIndexed {i, v -> IndexedValue(i, v)}.filter { it.value is T }
rocketraman
06/22/2016, 2:59 PMvoddan
06/22/2016, 2:59 PMvoddan
06/22/2016, 3:00 PMList<T>
rocketraman
06/22/2016, 3:01 PMilya.gorbunov
06/22/2016, 3:07 PMrocketraman
06/22/2016, 3:08 PM@Suppress("UNCHECKED_CAST")
fun <R> Iterable<*>.filterIsInstanceIndexed(clazz: Class<R>): List<IndexedValue<R>> =
mapIndexed {i, v -> IndexedValue(i, v)}
.filter { clazz.isInstance(it.value) } as List<IndexedValue<R>>