Thomas
02/15/2024, 3:35 AMlastIndex
(computed property) for Collection
in `Collections.kt`; lastIndex
is available for List
.
I see no reason why Collections
cannot have lastIndex
, when:
1. size
is a property declared by Collection
, so there is no issue with implementation, which is simply size - 1
.
2. indices
which calls 0..size - 1
under the hood is available for Collections
, so we have no issue with "last index might be something other than size-1".
Could there be something that I didn't consider?jw
02/15/2024, 3:39 AMThomas
02/15/2024, 3:42 AMindices
on Collections
?jw
02/15/2024, 3:53 AMmapIndexed { _, index -> index }
. I have definitely used indices
in the past for non-random access purposes, although I can't find specific examples right now.Klitos Kyriacou
02/15/2024, 9:48 AMIterable
types, including Collection
, have the method elementAt(index)
.Thomas
02/15/2024, 9:57 AMindices
should not be declared at Collection
Sam
02/16/2024, 2:04 PMindices
on an unordered collection like a set.Sam
02/16/2024, 2:04 PMjw
02/16/2024, 2:57 PMgrep
. This is only applying the map
operation, but I expect that to be the most common in the case where you want to map the ordinals to something.
./cashapp/redwood/redwood-tooling-codegen/src/main/kotlin/app/cash/redwood/tooling/codegen/widgetProtocolGeneration.kt:318: beginControlFlow("{ %L ->", trait.parameterTypes.indices.map { CodeBlock.of("arg$it") }.joinToCode())
./aosp/androidx-main/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/PopupViewHelper.kt:134: Layout.FLAT -> arrayOf(variants.indices.map { it + 1 }.toIntArray())
./aosp/androidx-main/sqlite/sqlite-inspection/src/androidTest/java/androidx/sqlite/inspection/test/GetSchemaTest.kt:194: alreadyOpenDatabases.indices.map { testEnvironment.receiveEvent().databaseOpened }
./aosp/androidx-main/sqlite/sqlite-inspection/src/androidTest/java/androidx/sqlite/inspection/test/TrackDatabasesTest.kt:91: val actual = expected.indices.map { testEnvironment.receiveEvent().databaseOpened }
./kotlin/kotlin/compiler/tests/org/jetbrains/kotlin/generators/tests/GenerateInRangeExpressionTestData.kt:80: val valNames = expressions.indices.map { "$prefix$it" }
jw
02/16/2024, 2:59 PM(0 until size).map
, of course, but might as well use what you gotSam
02/16/2024, 3:50 PMList(size) { … }
, but I can see the appeal of using indices
too. Thanks for sharing!