ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.05k stars 437 forks source link

JVM / Python / OOP like group_by view #1815

Open haleyk10198 opened 2 months ago

haleyk10198 commented 2 months ago

Hi,

I am writing to propose a view that mimics the behavior of group_by in other major OOP languages, docs: Kotlin, Scala, Python/Panda .

Schematically, the view is expected to have a signature of auto group_by(ranges::viewable_range R&& r, Pred&& pred)

where, return type should be an iterable of key-value tuples, where each value is also an iterable of ranges::range_value_t<R> Pred produces a key value (instead of boolean value)

The closest adaptor I know using STL is (omitting forward semantics for brevity), std::views::transform([](auto&& ele) { return std::tuple { Pred(ele), ele }; } | std::ranges::to<std::unordered_multimap>() | std::views::chunk_by(auto&& lhs, auto &&rhs) { std::get<0>(lhs) == std::get<0>(rhs); }

Though return type is a bit different it suffices for the use case: group by key, then do action on similar elements.

I am aware that chunk_by was previously named as group_by by FP conventions, and the name collision may be confusing.

As this adaptor is rather common in other OOP languages' standard library implementations, I would like to first check if this has been previously discussed in this repo.

Much thanks, for your attention, and your continued contribution to this repo. :)