groupFlatMap combinator

#1

Proposal to add groupFlatMap for every groupMap

def groupFlatMap[K, B](key: A => K)(f: A => IterableOnce[B]): immutable.Map[K, CC[B]]

is to

def groupMap[K, B](key: A => K)(f: A => B): immutable.Map[K, CC[B]]

as

def flatMap[B](f: A => IterableOnce[B]): CC[B]

is to

def map[B](f: A => B): CC[B]

Would groupFlatMapReduce also be useful?

After further thought, maybe its not obvious what would happen if IterableOnce is empty.

1 Like
#2

Can you provide some examples of when these would be useful?

#3

This can be (mostly) implemented in terms of groupMapReduce:

def groupFlatMap[A, K, B](as: Iterable[A])(key: A => K)(f: A => IterableOnce[B]): Map[K, Iterable[B]] =
  as.groupMapReduce(key)(a => View.from(f(a)))(_ ++ _)

It is not (yet?) possible to supply a Factory for building groups (here, I use View but it would be nice to build anything and in an efficient way), so the return type is Map[K, Iterable[B]] instead of something more specific.

Note that, as you noted, an important difference with the classic groupBy operations, is that with groupFlatMap groups can be empty.