Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a method to group flow elements in a time window #4069

Closed
SPC-code opened this issue Mar 17, 2024 · 3 comments
Closed

Provide a method to group flow elements in a time window #4069

SPC-code opened this issue Mar 17, 2024 · 3 comments

Comments

@SPC-code
Copy link

Use case

I have several cases where I need to group flow events by time and then combine them into something. The simplest example is averaging. If I have the real-time data, I frequently want to take all points in a given time window and return only averaged value for this time frame (or default value if no events happened in this time window). I have several such cases in VisionForge (visualization library) and Controls-kt (device data acquisition.

The Shape of the API

I would call it sample, but it is already taken, so it could be something like chunkedByTime to be aligned with stdlib method for collections. So, it could look like this: fun Flow<T>.chunkedByTime(duration: Duration): Flow<List<T>.

To make the API more universal one could provide external signal trigger. So, the collection is triggered by obtaining signal from a flow or channel.

Another API consideration is usage of API on numbers. For my purposes List is OK, it could be grouped later, which gives flexibility. But when we use List of numbers, it impacts performance. So, one could provide an inline method that transforms array of numbers internally in the flow evaluation loop. Like fun Flow<Double>.chunkedDoubleByTime(duration: Duration, collector: (DoubleArray)->R): Flow<Double>. Such API is opinionated, so I am not sure it should be in the library.

Prior Art

sample and debounce has similar functionality, but they return only one value, not all of them. I think the implementation could share some code.

@JakeWharton
Copy link
Contributor

JakeWharton commented Mar 17, 2024

#1302

@SPC-code
Copy link
Author

@JakeWharton Thanks, I tried to find it by enhancement tag. My issue is a duplicate, but maybe my use case would help.

@dkhalanskyjb
Copy link
Collaborator

Sure, it could be useful. Could you please add your use case under that issue?

Closing this issue, as we'd like to keep all pieces of the discussion centralized, so that we don't have to gather the information from all around the issue tracker.

@dkhalanskyjb dkhalanskyjb closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants