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

Add Function similar to Python's groupby() #1

Open
paulhoule opened this issue May 8, 2022 · 1 comment
Open

Add Function similar to Python's groupby() #1

paulhoule opened this issue May 8, 2022 · 1 comment
Labels

Comments

@paulhoule
Copy link
Owner

Python has a groupby function that works like the unix uniq command. It groups together a set of items together that share a common key into a single Iterable, and returns an Iterable of Pairs of keys and iterables. The signature would look something like

public static <X,K> Iterable<Pair<K,Iterable<X>> uniq(Function<X,K> keyFn, Iterable<X> source) {...}

if the source is sorted on the key first this is very similar to

collect(Collectors.groupBy(keyFn, collector))

except that the Streams API version keeps a hashtable of all the collectors throughout the process (uses memory) while the Python version just streams.

@paulhoule
Copy link
Owner Author

It is easy to feed the consecutive elements into an Iterator but returning an actual Iterable is not straightforward because an Iterable has to be restartable.

Possibly a better model is

public static <X,K,Y> Iterable<Pair<K,Y>> uniq(Function<X,K> keyFn, Collector<X,?,Z>) {
}

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

No branches or pull requests

1 participant