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

operator request: groupByOrdered #1529

Open
borissmidt opened this issue Jan 12, 2022 · 0 comments
Open

operator request: groupByOrdered #1529

borissmidt opened this issue Jan 12, 2022 · 0 comments

Comments

@borissmidt
Copy link

The operator def groupByOrdered[K](key: T => K): Observable[(K, Observable[T])] would be handy to check a stream until it changes.
You could see it as a repeated takeWhile while equivalence of K is used to check if the condition changed.

The same behavior can be made with a scan, but i quite often ends up as complex code because you have to filter out the intermediate values and have to wrap everything in an option to know the end of the stream.

The groupByOrdered would also make it easier to reduce by key more efficiently if you know your data comes in a known order.
For example when you query a database.

def reduceByKeyOrdered[T, K](key : T => K)(op: (T, T)) ={
    o.groupByOrdered(key).flatMap { case (_, group) =>
      group.reduce(select)
    }
}

Of course a more generic implementation could accept (previous: T, current: T) => Boolean to know when to break the current group.

I wouldn't mind to implement this operator if you think it is a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant