question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a `collectIndexed` terminal operator

See original GitHub issue

Hi,

Like with collections and sequences we occasionally need to iterate and maintain an index like this:

var index = 0
values.collect {
  // do something with the value, knowing its index.
  ++index
}

Since forEachIndexed already exists for Iterable and Sequence and consumeEachIndexed exists for ReceiveChannel it would seem natural to me that flows provide a collectIndexed terminal operator.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
elizarovcommented, Jun 5, 2019

@jcornaz When you say “we occasionally need” can you provide a slightly more concrete example where this might be needed when you work with flows, please. E.g. if you had recently run into this need in your project, you can explain what was the problem you were solving.

1reaction
fvascocommented, Jun 4, 2019

@jcornaz

I’d call it withIndex()

I agree.

it does not need to wrap each value

In escape analisys we hope

Read more comments on GitHub >

github_iconTop Results From Across the Web

collectIndexed - Kotlin
Terminal flow operator that collects the given flow with a provided action that takes the index of an element (zero-based) and the element....
Read more >
flow - GitHub Pages
Creates a cold flow from the given suspendable block. The flow being cold means that the block is called every time a terminal...
Read more >
Using LiveData & Flow in MVVM — Part III - ProAndroidDev
Because Flow is providing all of the data in the same coroutine context ... We can use the collect terminal operator for starting...
Read more >
A story of building a custom Flow operator - bufferTimeout
As collect is a terminal operation that will not finish until the upstream is finished, we need to put it in a coroutine,...
Read more >
Terminal Operators in Kotlin Flow - Amit Shekhar
My mission is to provide tech education to the students through project-based learning to achieve the outcome they aspire to.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found