Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Here's a solution for a lazy groupBy when the iterator contents are assumed to be clustered (repeated elements next to each other) over the key:

def clusteredGroupBy[B](h: Iterator[B])(f: B => _): Stream[Iterator[B]] = {
  if (h.hasNext) {
    val firstValue = h.next()
    val projection = f(firstValue)
    val (head, tail) = h.span(f(_) == projection)

    (Iterator(firstValue) ++ head) #:: clusteredGroupBy[B](tail)(f)
  } else Stream.empty
}

Any suggestions on how to improve this? Is there anything more native to Scala that does the same?

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.