Answering this question, Odomontois showed how you can implement a lazy group-by operator that can group a pre-sorted stream by a key without having to store the whole thing in memory. Is there any way to do something like this with Akka's streams (i.e. Source objects)? Alternatively, is there any way to pull out a regular Stream object from a Akka Source so I can use Odomontois's chopBy?
Here's an utterly failed attempt to do this that doesn't work:
implicit class SourceChopOps[T, NU](s: Source[T, NU]) {
def chopBy[U](f: T => U) = {
s.prefixAndTail(1)
.map(pt => (pt._1.head, pt._2))
.map {
case (prefix, tail) =>
// what to do with pulled off head???
tail.takeWhile(e => f(e) == f(prefix)) ++ tail.dropWhile(e => f(e) == f(prefix)).chopBy(f) // fails here
}
}
}
}