I think it could be great to add `countIndexed` an...
# stdlib
l
I think it could be great to add
countIndexed
and
sumByIndexed
to the stdlib. In an effort to remove a
var
and a
forEachIndexed
, I came across this need. However, I'd not want to bloat the stdlib. Please, add your if already needed one of these functions.
p
Just a note: I think it is better to use
foldIndexed
instead of
var
and
forEachIndexed
. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold-indexed.html
👍 4
l
Thanks for the suggestion, worked like a charm!
👍 1
m
You can always use
withIndex
and destructuring.
👍 2
list.withIndex().sumBy { (index, value) -> ... }
l
That's also a great option!
m
And more universal 😉