Jonathan Lennox
03/06/2023, 10:35 PMCLOVIS
03/06/2023, 10:37 PMJonathan Lennox
03/06/2023, 10:39 PMoverride val layerIds: Collection<Int>
get() = frameInfo?.let {
val ret = mutableListOf<Int>()
it.dti.forEachIndexed{ i, dti ->
if (dti != DTI.NOT_PRESENT)
ret.add(i)
}
ret
} ?: run { super.layerIds }
but that feels more procedural than Kotlin wantsJonathan Lennox
03/06/2023, 10:39 PMCLOVIS
03/06/2023, 10:40 PMJonathan Lennox
03/06/2023, 10:40 PMret
that I'm explicitly adding to, rather than some map or filter-ish thingCLOVIS
03/06/2023, 10:41 PMJonathan Lennox
03/06/2023, 10:41 PMCLOVIS
03/06/2023, 10:42 PMyourCollection.indices
gives you an iterable of the indices, not the elements.Jonathan Lennox
03/06/2023, 10:43 PMJonathan Lennox
03/06/2023, 10:43 PMget() = frameInfo?.let {
it.dti.indices.filter {i -> it.dti[i] != DTI.NOT_PRESENT }
} ?: run { super.layerIds }
CLOVIS
03/06/2023, 10:45 PMyourThing.withIndex()
.filter { (_, it) -> it != DTI.NOT_PRESENT }
.map { (index, _) -> index }
Jonathan Lennox
03/06/2023, 10:45 PMCLOVIS
03/06/2023, 10:46 PMPair<Int, YourType>
Jonathan Lennox
03/06/2023, 10:46 PMCLOVIS
03/06/2023, 10:47 PMCLOVIS
03/06/2023, 10:48 PMIndexedValue<T>
, not a Pair<Int, T>
, but well it's basically the same thingJonathan Lennox
03/06/2023, 10:48 PMwithIndex()
with its own method invocation rather than being like indices
which doesn't have such an invocation?CLOVIS
03/06/2023, 10:52 PMwithIndex()
(function) or indexed
(property). It seems more natural like this (but to my knowledge that's not a written rule).Jonathan Lennox
03/06/2023, 10:54 PMJonathan Lennox
03/06/2023, 10:54 PMCLOVIS
03/06/2023, 10:54 PMChris Fillmore
03/06/2023, 11:09 PMfoldIndexed
could also do this for you
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold-indexed.htmlephemient
03/07/2023, 12:56 AMephemient
03/07/2023, 12:59 AM.mapIndexedNotNull { i, dti -> if (dti != DTI.NOT_PRESENT) i else null }
ephemient
03/07/2023, 1:04 AMdti.indices.filter { dti[it] != DTI.NOT_PRESENT }
if the input is a List
Chris Fillmore
03/07/2023, 1:23 AMmapIndexedNotNull
but that’s because I usually reach for foldephemient
03/07/2023, 1:25 AMfold
have over
buildList {
list.forEachIndexed { i, dti ->
if (dti != DTI.NOT_PRESENT) add(i)
}
}
if you're adding to a mutable list?Chris Fillmore
03/07/2023, 1:26 AMbuildList
either, lolChris Fillmore
03/07/2023, 1:29 AMChris Fillmore
03/07/2023, 1:31 AMephemient
03/07/2023, 1:32 AM.foldIndexed(mutableListOf()) { i, acc, dti ->
if (dti != DTI.NOT_PRESENT) acc.add(i)
acc
}
the "accumulator" in scope is one which you would normally expect to vary from iteration to iteration in a normal fold, but it doesn't here. it's like an unnecessary var
in normal code.ephemient
03/07/2023, 1:32 AMChris Fillmore
03/07/2023, 1:34 AMChris Fillmore
03/07/2023, 1:36 AMindexesWhere(predicate)
phldavies
03/07/2023, 10:56 AMfun <T> Iterable<T>.indicesOf(predicate: (T) -> Boolean) =
buildSet { this@indicesOf.forEachIndexed { index, it -> if (predicate(it)) add(index) } }
Iterable<T>.indicesOf((T) -> Boolean): Iterable<Int>
might be better as you can toSet()
or toList()
etc as required (same probably applies to Sequence<T>.indicesOf((T) -> Boolean): Sequence<Int>
)ephemient
03/07/2023, 11:39 AMList
from Iterable
transformations (unless you're using one of the fooTo(destination)
functions). they run immediately and sequentially, having a Iterable
result doesn't make sensephldavies
03/07/2023, 11:42 AMIterable<T> -> List<T>
, but the Sequence extension would still apply. My reasoning here for Set<Int>
was ultimately that my use-cases required checking for existence within the set of matching indices.ephemient
03/07/2023, 11:43 AMinline fun <T> Iterable<T>.indicesOf(predicate: (T) -> Boolean): List<Int> = indicesOfTo(mutableListOf(), predicate)
inline fun <T, R : MutableCollection<in Int>> Iterable<T>.indicesOfTo(dest: R, predicate: (T) -> Boolean): R {
for ((i, elem) in this.withIndex()) if (predicate(elem)) dest.add(i)
return dest
}
and you could get your set by using .indicesOfTo(mutableSetOf()) { ... }
ephemient
03/07/2023, 11:44 AMSequence
, sure, although I think that would just not offer much over the .withIndex().filter().map()
or .mapIndexedNotNull()
mentioned earlierphldavies
03/07/2023, 11:45 AM.mapIndexedNotNullTo(mutableSetOf<Int>()) { ... }
but could just as easily have used that and cast to Set<Int>
to hide the mutability.