Skip to content

Instantly share code, notes, and snippets.

@lloydmeta
Last active December 17, 2015 19:49
Show Gist options
  • Save lloydmeta/5663011 to your computer and use it in GitHub Desktop.
Save lloydmeta/5663011 to your computer and use it in GitHub Desktop.
Pimp My Library: RichRange
package org.beachape.support
object RichRange {
implicit def range2RichRange(r: Range) = RichRange(r)
}
case class RichRange(range: Range) {
def listOfConsecutivePairsInSteps(step: Int) = {
val steppedRange = range by step
def splitIntoConsecutivePairs(xs: List[Int]):List[(Int,Int)] = {
xs match {
case x :: y :: Nil => {
if (y != range.end) {
List((x, range.end))
} else {
List((x, y))
}
}
case x :: y :: ys => List((x,y)) ::: splitIntoConsecutivePairs(y::ys)
case x :: Nil => List((x, range.end))
case _ => Nil
}
}
splitIntoConsecutivePairs(steppedRange.toList)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment