Skip to content

Instantly share code, notes, and snippets.

@TJC
Created March 1, 2012 02:02
Show Gist options
  • Save TJC/1946649 to your computer and use it in GitHub Desktop.
Save TJC/1946649 to your computer and use it in GitHub Desktop.
melb scala ranges challenge
def ranges(ints: Seq[Int]): Seq[(Int, Int)] = {
ints.sorted.foldLeft( Seq[(Int,Int)]() )(
(acc: Seq[(Int,Int)], n: Int) => acc match {
// case Nil => acc :+ (n,n)
case x :: (a,z) if (z == n) => acc
case x :: (a,z) if (z+1 == n) => x :+ (a, n)
case _ => acc :+ (n,n)
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment