Skip to content

Instantly share code, notes, and snippets.

@lloydmeta
Created May 28, 2013 12:29
Show Gist options
  • Save lloydmeta/5662413 to your computer and use it in GitHub Desktop.
Save lloydmeta/5662413 to your computer and use it in GitHub Desktop.
Returns a List of consecutive Doubles based on a start and end and a step value. Guarantees the end value is in the last double.
def listOfConsecutivePairsInRange(start: Double, end: Double, step: Double = 1) = {
val initialRange = start to end by step
def splitIntoConsecutivePairs(xs: List[Double]):List[(Double,Double)] = {
xs match {
case x::y::Nil => {
if (y != end) {
List((x, end))
} else {
List((x, y))
}
}
case x::y::ys => List((x,y)) ::: splitIntoConsecutivePairs(y::ys)
case _ => Nil
}
}
splitIntoConsecutivePairs(initialRange.toList)
}
// Example
listOfConsecutivePairsInRange(1000, 3000, 300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment